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 ?
with open("yourlogfile.log", "w") as log_file:
pprint.pprint(dataobject, log_file)
See the documentation .
Please use pprint.pformat
, which returns a formated string that can be dumped directly to file.
>>> import pprint
>>> with open("file_out.txt", "w") as fout:
... fout.write(pprint.pformat(vars(pprint)))
...
Reference:
For Python 2.7
logFile = open('c:\\temp\\mylogfile'+'.txt', 'w')
pp = pprint.PrettyPrinter(indent=4, stream=logFile)
pp.pprint(dataobject) #you can reuse this pp.print
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.