[英]how to send the output of pprint module to a log file
我有以下代码:
logFile=open('c:\\temp\\mylogfile'+'.txt', 'w')
pprint.pprint(dataobject)
如何以漂亮的打印格式将数据对象的内容发送到日志文件?
with open("yourlogfile.log", "w") as log_file:
pprint.pprint(dataobject, log_file)
请参阅文档。
请使用pprint.pformat
,它返回一个可以直接转储到文件的格式化字符串。
>>> import pprint
>>> with open("file_out.txt", "w") as fout:
... fout.write(pprint.pformat(vars(pprint)))
...
参考:
对于 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.