I have the following code:
logFile=open('c:\\temp\\mylogfile'+'.txt', 'w') pprint.pprint(dataobject)
how can i send the contents of dataobject to the log file on the pretty print format ?
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() .
pprint() − prints the formatted representation of PrettyPrinter object. pformat() − Returns the formatted representation of object, based on parameters to the constructor. Following example demonstrates simple use of PrettyPrinter class.
The pprint module in Python is a utility module that you can use to print data structures in a readable, pretty way. It's a part of the standard library that's especially useful for debugging code dealing with API requests, large JSON files, and data in general.
This article is about a pretty useful built-in module in Python, pprint.
with open("yourlogfile.log", "w") as log_file: pprint.pprint(dataobject, log_file)
See the documentation.
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