What you need is Pretty Print pprint module:

from pprint import pprint

# Build the tree somehow

with open('output.txt', 'wt') as out:
    pprint(myTree, stream=out)
Answer from Stefano Sanfilippo on Stack Overflow
🌐
Python
docs.python.org › 3 › library › pprint.html
pprint — Data pretty printer
The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter. If the formatted structures include objects which are not fundamental Python types, the ...
Discussions

How to prettyprint a JSON file?
This method returns a string, making ... to a file. ... Improved Readability: Easier for humans to read and debug. Organized Structure: Helps identify nested elements and their relationships. Pretty-printing is essential when dealing with complex data structures or when JSON needs to be logged or presented in a human-readable format. ... how to exit a python script in ... More on janbasktraining.com
🌐 janbasktraining.com
1
April 29, 2025
python 3.x - How to get a pretty print for my input file - Stack Overflow
I have a file that contains data as shown in the Input file below. My program reads the config file, and writes section of this file as individual files. I read this file using python3 and pyyaml More on stackoverflow.com
🌐 stackoverflow.com
Pretty-Print JSON Data to a File using Python - Stack Overflow
A project for class involves parsing Twitter JSON data. I'm getting the data and setting it to the file without much trouble, but it's all in one line. This is fine for the data manipulation I'm tr... More on stackoverflow.com
🌐 stackoverflow.com
prettyprint is not pretty when trying to write to text file
Second the json recommendation -- it can even let you do indents, although that is not on by default. Have you tried pprint.pformat(object, indent=1, width=80, depth=None)? More on reddit.com
🌐 r/learnpython
8
2
October 18, 2014
🌐
Real Python
realpython.com › python-pretty-print
Prettify Your Data Structures With Pretty Print in Python – Real Python
March 18, 2026 - However, you can also use pprint() to send pretty outputs to files and have these act as logs if you prefer. Although dictionaries are generally considered unordered data structures, since Python 3.6, dictionaries are ordered by insertion. pprint() ...
🌐
Better Stack
betterstack.com › community › questions › how-to-print-json-file-in-python
How to Prettyprint a Json File in Python? | Better Stack Community
import json # Load JSON data from a file with open('file.json', 'r') as file: json_data = json.load(file) # Pretty print the JSON data pretty_json = json.dumps(json_data, indent=4) print(pretty_json) ...
🌐
Analytics Vidhya
analyticsvidhya.com › home › how to use pprint in python? [explained with examples]
How to Use PPrint in Python? [Explained with Examples]
October 19, 2024 - A. Pprint (pretty print) is a Python module used for formatting complex data structures more readably and organized, especially when printing them to the console or writing to a file.
🌐
seenode blog
seenode.com › blog › how-to-use-python-pretty-print-pprint
How to Use Python Pretty Print (pprint) for Cleaner Python Output | seenode blog
May 19, 2025 - It refers to “Pretty Print,” the native Python library that allows developers to customize their output experience with its different parameters and flags for a single class print (). This class effectively uses six parameters.
🌐
PYnative
pynative.com › home › python › json › python prettyprint json data
Python PrettyPrint JSON Data
May 14, 2021 - The json.tool module also provides a outfile command-line option to write validated and pretty-printed JSON data into a new file. ... A new file is created, which contains prettyPrinted JSON data. I want to hear from you. What do you think of this article? Or maybe I missed one of the ways to PrettyPrint JSON data in Python.
Find elsewhere
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › how to pretty print a json file in python?
How to Pretty Print a JSON file in Python? - Spark By {Examples}
May 31, 2024 - The dumps() from the json module & pprint() from the pprint module are used to pretty print a JSON file in Python, JSON data is often stored in a
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pretty-print-json
Python - Pretty Print JSON - GeeksforGeeks
July 23, 2025 - To write a Python object as JSON Pretty Print format data into a file, json.dump() method is used.
🌐
PythonHow
pythonhow.com › how › prettyprint-a-json-file
Here is how to prettyprint a JSON file in Python
Then, json.dumps() is used to convert the object back to a formatted JSON string with an indentation level of 4. Finally, you can either print the pretty printed JSON or save it to a file as per your requirement. By specifying the indent parameter in json.dumps(), you can control the number of spaces used for indentation. Setting indent to None or 0 will result in compact JSON without any extra whitespace. Note that if the JSON file is already loaded into a Python object, you can directly pass that object to json.dumps() instead of loading it from a file.
🌐
Finxter
blog.finxter.com › home › learn python blog › python pprint – a simple guide to pretty printing
Python pprint - A Simple Guide to Pretty Printing - Be on the Right Side of Change
October 18, 2022 - 💬 Question: How to get Python’s pprint() method to return a string instead of printing it to the standard output? To pretty-print to a string instead of the standard output, you can use the pprint.pformat() function instead of pprint.pprint().
🌐
Medium
medium.com › codex › prettify-your-python-outputs-using-pprint-60522ef997d0
Prettify your Python Outputs using pprint()! | by Satyam Gupta | CodeX | Medium
August 2, 2022 - The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter. If the formatted structures include objects which are not fundamental Python types, the ...
🌐
Python Module of the Week
pymotw.com › 2 › pprint
pprint – Pretty-print data structures - Python Module of the Week
It formats your object and writes it to the data stream passed as argument (or sys.stdout by default). from pprint import pprint from pprint_data import data print 'PRINT:' print data print print 'PPRINT:' pprint(data) $ python pprint_pprint.py PRINT: [(0, {'a': 'A', 'c': 'C', 'b': 'B', 'e': 'E', 'd': 'D', 'g': 'G', 'f': 'F', 'h': 'H'}), (1, {'a': 'A', 'c': 'C', 'b': 'B', 'e': 'E', 'd': 'D', 'g': 'G', 'f': 'F', 'h': 'H'}), (2, {'a': 'A', 'c': 'C', 'b': 'B', 'e': 'E', 'd': 'D', 'g': 'G', 'f': 'F', 'h': 'H'})] PPRINT: [(0, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H'}), (1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H'}), (2, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H'})]
🌐
Better Programming
betterprogramming.pub › how-to-pretty-print-in-python-9b1d8764d151
How To Pretty Print in Python. Start using the pprint library | by Jonathan Hsu | Better Programming
December 5, 2019 - Refer to the official documentation for a full list of library and all properties available. In this tutorial we’ll go over basic usage of the pprint() method and PrettyPrinter() class. To use pprint, begin by importing the library at the ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › pprint-data-pretty-printer-python
pprint : Data pretty printer in Python - GeeksforGeeks
The pprint module (pretty print) is a built-in utility that formats complex data structures like dictionaries, lists, or JSON in a readable way with proper indentation and line breaks.
Published: January 9, 2026
🌐
Python
docs.python.org › 3.4 › library › pprint.html
8.11. pprint — Data pretty printer — Python 3.4.10 documentation
An output stream may be set using the stream keyword; the only method used on the stream object is the file protocol’s write() method. If not specified, the PrettyPrinter adopts sys.stdout. The amount of indentation added for each recursive level is specified by indent; the default is one. Other values can cause output to look a little odd, but can make nesting easier to spot. The number of levels which may be printed is controlled by depth; if the data structure being printed is too deep, the next contained level is replaced by ....
🌐
Reddit
reddit.com › r/learnpython › prettyprint is not pretty when trying to write to text file
r/learnpython on Reddit: prettyprint is not pretty when trying to write to text file
October 18, 2014 -

I have a very big dictionary I would like to write to a text file. The dictionary in question has values which are lists of very long strings. When I used pprint to write to a text file, I expect something similar to:

{"key0":
        ["value1"],
 "key1":
        ["value2"]....
}

But instead I get a giant mess that totally defeated the purpose of using pprint in the first place. I even tried using a third-party pprintpp and that didnt work either. Does anyone have any idea of how to do this?