I'm using this gist's tree, and now I'm trying to figure out how to prettyprint to a file. Any tips?
To use pprint, begin by importing the library at the top of your Python file. From here you can either use the . pprint() method or instantiate your own pprint object with PrettyPrinter() .
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)
Another general-purpose alternative is Pretty Print's pformat()
method, which creates a pretty string. You can then send that out to a file. For example:
import pprint data = dict(a=1, b=2) output_s = pprint.pformat(data) # ^^^^^^^^^^^^^^^ with open('output.txt', 'w') as file: file.write(output_s)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With