pprint --- 資料美化列印器¶
原始碼:Lib/pprint.py
The pprint module provides a capability to "pretty-print" arbitrary
Python data structures in a form which can be used as input to the interpreter.
If the formatted structures include objects which are not fundamental Python
types, the representation may not be loadable. This may be the case if objects
such as files, sockets or classes are included, as well as many other
objects which are not representable as Python literals.
The formatted representation keeps objects on a single line if it can, and breaks them onto multiple lines if they don't fit within the allowed width, adjustable by the width parameter defaulting to 80 characters.
在 3.9 版的變更: Added support for pretty-printing types.SimpleNamespace.
在 3.10 版的變更: Added support for pretty-printing dataclasses.dataclass.
函式¶
- pprint.pp(object, stream=None, indent=1, width=80, depth=None, *, compact=False, sort_dicts=False, underscore_numbers=False)¶
Prints the formatted representation of object, followed by a newline. This function may be used in the interactive interpreter instead of the
print()function for inspecting values. Tip: you can reassignprint = pprint.ppfor use within a scope.- 參數:
object -- 將被印出的物件。
stream (file-like object | None) -- A file-like object to which the output will be written by calling its
write()method. IfNone(the default),sys.stdoutis used.indent (int) -- The amount of indentation added for each nesting level.
width (int) -- The desired maximum number of characters per line in the output. If a structure cannot be formatted within the width constraint, a best effort will be made.
depth (int | None) -- The number of nesting levels which may be printed. If the data structure being printed is too deep, the next contained level is replaced by
.... IfNone(the default), there is no constraint on the depth of the objects being formatted.compact (bool) -- Control the way long sequences are formatted. If
False(the default), each item of a sequence will be formatted on a separate line, otherwise as many items as will fit within the width will be formatted on each output line.sort_dicts (bool) -- If
True, dictionaries will be formatted with their keys sorted, otherwise they will be displayed in insertion order (the default).underscore_numbers (bool) -- If
True, integers will be formatted with the_character for a thousands separator, otherwise underscores are not displayed (the default).
>>> import pprint >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni'] >>> stuff.insert(0, stuff) >>> pprint.pp(stuff) [<Recursion on list with id=...>, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']
在 3.8 版被加入.
- pprint.pprint(object, stream=None, indent=1, width=80, depth=None, *, compact=False, sort_dicts=True, underscore_numbers=False)¶
Alias for
pp()with sort_dicts set toTrueby default, which would automatically sort the dictionaries' keys, you might want to usepp()instead where it isFalseby default.
- pprint.pformat(object, indent=1, width=80, depth=None, *, compact=False, sort_dicts=True, underscore_numbers=False)¶
Return the formatted representation of object as a string. indent, width, depth, compact, sort_dicts and underscore_numbers are passed to the
PrettyPrinterconstructor as formatting parameters and their meanings are as described in the documentation above.
- pprint.isreadable(object)¶
Determine if the formatted representation of object is "readable", or can be used to reconstruct the value using
eval(). This always returnsFalsefor recursive objects.>>> pprint.isreadable(stuff) False
- pprint.isrecursive(object)¶
Determine if object requires a recursive representation. This function is subject to the same limitations as noted in
saferepr()below and may raise anRecursionErrorif it fails to detect a recursive object.
- pprint.saferepr(object)¶
Return a string representation of object, protected against recursion in some common data structures, namely instances of
dict,listandtupleor subclasses whose__repr__has not been overridden. If the representation of object exposes a recursive entry, the recursive reference will be represented as<Recursion on typename with id=number>. The representation is not otherwise formatted.>>> pprint.saferepr(stuff) "[<Recursion on list with id=...>, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']"
PrettyPrinter 物件¶
- class pprint.PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, compact=False, sort_dicts=True, underscore_numbers=False)¶
建立一個
PrettyPrinter實例。Arguments have the same meaning as for
pp(). Note that they are in a different order, and that sort_dicts defaults toTrue.>>> import pprint >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni'