Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send the output of pprint module to a log file

Tags:

python

logging

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 ?

like image 942
AKM Avatar asked Oct 07 '10 09:10

AKM


People also ask

How do I use Pprint in Python?

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 is Pprint Pformat?

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.

Is Pprint part of Python?

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.

Is Pprint built in function in Python?

This article is about a pretty useful built-in module in Python, pprint.


1 Answers

with open("yourlogfile.log", "w") as log_file:     pprint.pprint(dataobject, log_file) 

See the documentation.

like image 132
livibetter Avatar answered Sep 30 '22 10:09

livibetter