Prettyprint to a file?
What you need is Pretty Print pprint
module:
from pprint import pprint# Build the tree somehowwith 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 pprintdata = dict(a=1, b=2)output_s = pprint.pformat(data)# ^^^^^^^^^^^^^^^with open('output.txt', 'w') as file: file.write(output_s)
If I understand correctly, you just need to provide the file to the stream
keyword on pprint:
with open(outputfilename,'w') as fout: pprint(tree,stream=fout,**other_kwargs)