Good Python programmers learn to share with pprint route instead of print

Python programmers learn to share good route instead of print friendly print debug information pprint
pprint is "pretty printer" shorthand, "pretty" means "beautiful, beautiful" and therefore the meaning of pprint is: a beautiful print .

This is a fairly simple but with modules, mainly for printing objects complex data structures, such as nested list of tuples, and dictionaries.

Take a look at print () for printing an example:

mylist = ["Beautiful is better than ugly.", "Explicit is better than implicit.", "Simple is better than complex.", "Complex is better than complicated."]

print(mylist)

['Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.']

This is a simple example, all printed on one line. If the object element is nested content (such as complex data dictionary), then print out the mess it is certainly not good reading.

Use pprint module pprint () alternate print (), pain points can be resolved as follows:

  • Suitable provided width of the line, make appropriate linefeed
  • Set the indent printing hierarchy format printing
  • Is there an infinite loop to determine the object, and optimize print

Basic use

pprint(object, stream=None, indent=1, width=80, depth=None, *,compact=False)

The default parameters for the line width of 80, when the printed characters is less than 80, the pprint () built-in function substantially equivalent to Print (), when a character is exceeded, it will beautify as, formatted output.

import pprint

mylist = ["Beautiful is better than ugly.", "Explicit is better than implicit.", "Simple is better than complex.", "Complex is better than complicated."]

pprint.pprint(mylist)

# 超出80字符,打印的元素是换行的
['Beautiful is better than ugly.',
 'Explicit is better than implicit.',
 'Simple is better than complex.',
 'Complex is better than complicated.']

Set the indent

pprint.pprint(mylist, indent=4)

[   'Beautiful is better than ugly.',
    'Explicit is better than implicit.',
    'Simple is better than complex.',
    'Complex is better than complicated.']

Set the print line width

  • mydict = {'students': [{'name':'Tom', 'age': 18},{'name':'Jerry', 'age': 19}]}
    
    pprint.pprint(mydict)
    
    # 正常打印
    {'students': [{'age': 18, 'name': 'Tom'}, {'age': 19, 'name': 'Jerry'}]}
    
    pprint.pprint(mydict, width=20)
    
    # 行宽为 20
    {'students': [{'age': 18,
                 'name': 'Tom'},
                {'age': 19,
                 'name': 'Jerry'}]}
    
    pprint.pprint(mydict, width=70)
    
    # 行宽为 70
    {'students': [{'age': 18, 'name': 'Tom'},
                {'age': 19, 'name': 'Jerry'}]}

Set the print level

newlist = [1, [2, [3, [4, [5]]]]]

pprint.pprint(newlist, depth=3)

# 超出的层级会用 ... 表示
[1, [2, [3, [...]]]]

Replace print with pprint

import pprint
print = pprint.pprint

mylist = ["Beautiful is better than ugly.", "Explicit is better than implicit.", "Simple is better than complex.", "Complex is better than complicated."]

print(mylist)

['Beautiful is better than ugly.',
 'Explicit is better than implicit.',
 'Simple is better than complex.',
 'Complex is better than complicated.']

More usage please see the official documentation
Overall, pprint () is a print () lightweight alternative, simple and practical, very convenient, but also Python standard library.

Guess you like

Origin blog.51cto.com/14479068/2440778