Tutorial for formatting output using the pprint function in Python
- 2020-05-05 11:29:04
- OfStack
pprint print
Effect: beautiful print data structure
pprint includes a "aesthetics printer" that generates a aesthetics view of the data structure. The formatter generates some representation of the data structure that is not only parsed correctly by the interpreter, but also easy for humans to read. The output is placed on as many lines as possible, and is indented when broken into multiple lines.
The following example USES data to contain the data
data = [(1,{'a':'A','b':'B','c':'C','d':'D'}),
(2,{'e':'E','f':'F','g':'G','h':'H',
'i':'I','j':'J','k':'K','l':'L'
}),
]
1, print
The easiest way to use this module is to use the pprint() function
from pprint import pprint
print 'PRINT:'
print data
print
print 'PPRINT:'
pprint(data)
Results:
PRINT:
[(1, {'a': 'A', 'c': 'C', 'b': 'B', 'd': 'D'}), (2, {'e': 'E', 'g': 'G', 'f': 'F', 'i': 'I', 'h': 'H', 'k': 'K', 'j': 'J', 'l': 'L'})]
PPRINT:
[(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}),
(2,
{'e': 'E',
'f': 'F',
'g': 'G',
'h': 'H',
'i': 'I',
'j': 'J',
'k': 'K',
'l': 'L'})]
pprint() formats an object and writes it to a data stream, which is passed in as a parameter (or the default sys.stdout)
Notice why there is a column in the second dictionary, because pprint prints
for columns that support more than 8 objects2, format
To format a data structure without writing it directly to a stream (for example, for logging), you can use pformat() to construct a string representation.
import logging
from pprint import pformat
logging.basicConfig(level = logging.DEBUG,
format = '%(levelname)-8s %(message)s',
)
logging.debug('Logging pformatted data')
formatted = pformat(data)
for line in formatted.splitlines():
logging.debug(line.rstrip())
Results:
DEBUG Logging pformatted data
DEBUG [(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}),
DEBUG (2,
DEBUG {'e': 'E',
DEBUG 'f': 'F',
DEBUG 'g': 'G',
DEBUG 'h': 'H',
DEBUG 'i': 'I',
DEBUG 'j': 'J',
DEBUG 'k': 'K',
DEBUG 'l': 'L'})]
You can then print the formatted string separately at a low rate or count it into the log
splitlines() split by line ()
rstrip() removes Spaces on the right, lstrip() removes Spaces on the left, strip() removes Spaces on both sides. The default is to remove Spaces. You can also pass in characters that need to be removed from either side or one of them, such as strip(' a'), which removes the character 'a'
from both sides of the string
3, any class
The PrettyPrinter class used by pprint() can also handle these custom classes if a custom class defines a method with s 115en__ ().
from pprint import pprint
class node(object):
def __init__(self,name,contents =[]):
self.name = name
self.contents = contents[:]
def __repr__(self):
return ('node(' + repr(self.name) + ',' +
repr(self.contents) + ')'
)
trees = [node('node-1'),
node('node-2',[node('node-2-1')]),
node('node-3',[node('node-3-1')]),
]
pprint(trees)
Result:
[node('node-1',[]),
node('node-2',[node('node-2-1',[])]),
node('node-3',[node('node-3-1',[])])]
The representation of the nested object is combined by PrettyPrinter to return the full string representation.
4, recursion
A recursive data structure is represented by a reference to the original data source in the form <
Recursion on typename with id=number
>
.
from pprint import pprint
local_data = ['a','b',1,2]
local_data.append(local_data)
print 'id(local_data) =>',id(local_data)
pprint(local_data)
print local_data
Result:
id(local_data) => 47458332363520
['a', 'b', 1, 2, <Recursion on list with id=47458332363520>]
['a', 'b', 1, 2, [...]]
In this example, the list local_data is added to itself, creating a recursive reference to
The built-in function id() gets the id value of the object. Theoretically, each object has an id value. If it is an integer and a string (when relatively small), the same value will have the same id value, but if it is a class, the same value will have a different id value. The test is as follows:
#int or float or lon Are all the same ( When I was younger )
a = 65464131311513l
b = 65464131311513l
c = 65464131311513l
print id(a)
print id(b)
print id(c)
print
a = '12312312'
b = '12312312'
c = '12312312'
print id(a)
print id(b)
print id(c)
print
a = 65464131311513l*11
b = 65464131311513l*11
c = 65464131311513l*11
print id(a)
print id(b)
print id(c)
print
a = '12312312'*11
b = '12312312'*11
c = '12312312'*11
print id(a)
print id(b)
print id(c)
print
class Test(object):
def __init__(self):
pass
a = Test()
b = Test()
c = Test()
print id(a)
print id(b)
print id(c)
print
Test results:
from pprint import pprint
print 'PRINT:'
print data
print
print 'PPRINT:'
pprint(data)
0
5, limits nested output
For very deep data structures, the output may not be required to contain all the details. It is possible that the data is not locally formatted, that the formatted text is too large to be managed, or that it is redundant when writing the data from memory.
from pprint import pprint
print 'PRINT:'
print data
print
print 'PPRINT:'
pprint(data)
1
Result:
from pprint import pprint
print 'PRINT:'
print data
print
print 'PPRINT:'
pprint(data)
2
The depth parameter can be used to control the depth of nested data structures recursively processed by the aesthetic printer. Levels not included in the output are indicated by an ellipsis for
6, controls the output width
The default output width for formatted text is 80 columns. To adjust this width, you can use the parameter width in pprint().
from pprint import pprint
for width in [80,5]:
print 'WIDTH = ', width
pprint(data,width = width)
print
Results:
from pprint import pprint
print 'PRINT:'
print data
print
print 'PPRINT:'
pprint(data)
4
When the width is not large enough to format a data structure, truncation or line transition is not possible if it introduces an illegal syntax.