An overview of the
When we use the built-in print function print, the Python data structure objects are always printed in a single line. This makes the display of objects with complex data structures or more data unattractive. In this case, we can use pprint output to beautify the data structure objects.
An overview of pprint methods
Beautify the output
Class pprint.PrettyPrinter (indent = 1, width = 80, depth = None, Stream = None, *, compact = False)
- Indent the indentation
- Width the width of the
- Depth Print depth
- Stream refers to the output stream object,
stream = None
The output stream object defaults tosys.stdout
- Compact If Compact is false (the default), each item in a long sequence is formatted on a separate line. If Compact is true, a width-appropriate item is formatted on each output line.
import pprint
L = [str(i)*20 for i in range(10)]
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(L)
print(L)
Copy the code
Example results:
[ '00000000000000000000'.'11111111111111111111'.'22222222222222222222'.'33333333333333333333'.'44444444444444444444'.'55555555555555555555'.'66666666666666666666'.'77777777777777777777'.'88888888888888888888'.'99999999999999999999']
['00000000000000000000'.'11111111111111111111'.'22222222222222222222'.'33333333333333333333'.'44444444444444444444'.'55555555555555555555'.'66666666666666666666'.'77777777777777777777'.'88888888888888888888'.'99999999999999999999']
Copy the code
Object string
We can also return the formatted representation of the target object as a string. Pformat (*object*, *indent = 1*, *width = 80*, *depth = None*, ***, * compact = False *) [¶] “permanent link to the target” (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)
Copy the code
Example results:
[ '00000000000000000000'.'11111111111111111111'.'22222222222222222222'.'33333333333333333333'.'44444444444444444444'.'55555555555555555555'.'66666666666666666666'.'77777777777777777777'.'88888888888888888888'.'99999999999999999999']
['00000000000000000000'.'11111111111111111111'.'22222222222222222222'.'33333333333333333333'.'44444444444444444444'.'55555555555555555555'.'66666666666666666666'.'77777777777777777777'.'88888888888888888888'.'99999999999999999999']
Copy the code
Formatted print
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)
Copy the code
Example results:
[ '00000000000000000000'.'11111111111111111111'.'22222222222222222222'.'33333333333333333333'.'44444444444444444444'.'55555555555555555555'.'66666666666666666666'.'77777777777777777777'.'88888888888888888888'.'99999999999999999999']
['00000000000000000000'.'11111111111111111111'.'22222222222222222222'.'33333333333333333333'.'44444444444444444444'.'55555555555555555555'.'66666666666666666666'.'77777777777777777777'.'88888888888888888888'.'99999999999999999999']
Copy the code
readability
Check whether the string object is readable, True, and vice versa.
import pprint
L = [str(i)*20 for i in range(10)]
B = pprint.isreadable(L)
print(B)
Copy the code
Example results:
True
Copy the code