pprint Python3 built-in modules allow printing of more beautiful than print

Outline

When we use the built-in function Print print, print out the data structure of the Python line object is always outputted in such a complicated structure of the display data or the data object is not more beautiful, then we can use the output pprint landscaping data structure object.

pprint method Overview

Beautify output

Pprint we can use the PrettyPrintercontrol output of the indent printing, line width, depth and even printing, the class is defined as followsclass pprint.PrettyPrinter(indent = 1,width = 80,depth = None,stream = None,*,compact = False )

  • indent indent
  • width width
  • depth print darkness
  • refers to the output stream object stream, stream = Nonethe output stream object defaultssys.stdout
  • If the compact compact is false (the default), then the sequence length of each item will be formatted on a separate line. If the compact is true, the format for each output line width of an item.
import pprint

L = [str(i)*20 for i in range(10)]
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(L)
print(L)
复制代码

Sample results:

[   '00000000000000000000',
    '11111111111111111111',
    '22222222222222222222',
    '33333333333333333333',
    '44444444444444444444',
    '55555555555555555555',
    '66666666666666666666',
    '77777777777777777777',
    '88888888888888888888',
    '99999999999999999999']
['00000000000000000000', '11111111111111111111', '22222222222222222222', '33333333333333333333', '44444444444444444444', '55555555555555555555', '66666666666666666666', '77777777777777777777', '88888888888888888888', '99999999999999999999']
复制代码

String Object

We can also format the target object returns a string representation. indentwidth , depth and compact will PrettyPrinter be passed as a parameter to the constructor format, the class is defined as follows pprint.pformat(*object*,*indent = 1*,*width = 80*,*depth = None*,***,*compact = False *)[¶](https://docs.python.org/zh-cn/3.7/library/pprint.html#pprint.pformat "永久链接至目标")

L = [str(i)*20 for i in range(10)]
pp = pprint.pformat(L, indent=4)
print(pp)
print(L)
复制代码

Sample results:

[   '00000000000000000000',
    '11111111111111111111',
    '22222222222222222222',
    '33333333333333333333',
    '44444444444444444444',
    '55555555555555555555',
    '66666666666666666666',
    '77777777777777777777',
    '88888888888888888888',
    '99999999999999999999']
['00000000000000000000', '11111111111111111111', '22222222222222222222', '33333333333333333333', '44444444444444444444', '55555555555555555555', '66666666666666666666', '77777777777777777777', '88888888888888888888', '99999999999999999999']
复制代码

Format Printing

Target character string to a specified output format of the output stream, and finally terminated by a newline, as defined class pprint.pprint(object,stream = None,indent = 1,width = 80,depth = None,*,compact = False )

import pprint

L = [str(i)*20 for i in range(10)]
pprint.pprint(L, indent=4)
print(L)
复制代码

Sample results:

[   '00000000000000000000',
    '11111111111111111111',
    '22222222222222222222',
    '33333333333333333333',
    '44444444444444444444',
    '55555555555555555555',
    '66666666666666666666',
    '77777777777777777777',
    '88888888888888888888',
    '99999999999999999999']
['00000000000000000000', '11111111111111111111', '22222222222222222222', '33333333333333333333', '44444444444444444444', '55555555555555555555', '66666666666666666666', '77777777777777777777', '88888888888888888888', '99999999999999999999']
复制代码

readability

Determining whether the target object readable string object, Truereadable, anti otherwise.

import pprint

L = [str(i)*20 for i in range(10)]
B = pprint.isreadable(L)
print(B)
复制代码

Sample results:

True
复制代码

Reproduced in: https: //juejin.im/post/5cee8ebb518825332550ce0a

Guess you like

Origin blog.csdn.net/weixin_33817333/article/details/91447127