pprint module提供了可以按照某个格式正确的显示python已知类型数据的一种方法,这种格式可被解析器解析, 又很易读。但是,如果已知格式的数据对象不是python的基础类型,这种表示方法就有可能加载失败。这种情况一般是对象为 files, sockets, classes, or instances are included, as well as many other built-in objects which are not representable as Python constants。
该方法输出将对象的输出分隔成单行显示,并在宽度设置不适合时,将其分成多行显示。
class pprint.PrettyPrinter(indent=1,width=80,depth=None,stream=None)
-
pprint.
pformat
(
object,
indent=1,
width=80,
depth=None
)仅仅想获得数据而不是输出数据也可以用pformat
-
Return the formatted representation of object as a string. indent,widthanddepth will be passed to thePrettyPrinter constructor as formatting parameters.
-
Prints the formatted representation of object on stream, followed by anewline.