How can I force pprint() to print one list/tuple/dict element per line?
>>> from pprint import pprint
>>> my_var = ['one', 'two', ('red','green'), {'state' : 'Oregon', 'city' : 'Portland'}]
>>> pprint(my_var)
['one', 'two', ('red', 'green'), {'city': 'Portland', 'state': 'Oregon'}]
I would like the output to be something like:
['one',
'two',
('red',
'green'),
{'city': 'Portland',
'state': 'Oregon'}]
Use pprint() in place of the regular print() Understand all the parameters you can use to customize your pretty-printed output. Get the formatted output as a string before printing it. Create a custom instance of PrettyPrinter.
Use pprint() to Pretty Print a Dictionary in Python Within the pprint module there is a function with the same name pprint() , which is the function used to pretty-print the given string or object. First, declare an array of dictionaries. Afterward, pretty print it using the function pprint.
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.
Use a width=1 argument to pprint():
>>> from pprint import pprint
>>> my_var = ['one', 'two', ('red','green'), {'state' : 'Oregon', 'city' : 'Portland'}]
>>> pprint(my_var, width=1)
['one',
'two',
('red',
'green'),
{'city': 'Portland',
'state': 'Oregon'}]
>>>
"pprint - Data pretty printer" 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