pprint 模块(数据漂亮打印机)
pprint 模块 (lib/pprint.py) 是 Python 标准库的一部分,随标准 Python 发行版一起发布。pprint 代表漂亮打印机。pprint 模块的功能使 Python 数据结构具有美观的外观。任何可被 Python 解释器正确解析的数据结构都经过优雅的格式化。格式化的表达式尽可能保持在一行中,但如果长度超过格式化的宽度参数,则会分成多行。pprint 输出的一个独特功能是,在格式化显示表示之前会自动对字典进行排序。
pprint 模块包含 PrettyPrinter 类的定义。其构造函数采用以下格式 −
pprint.PrettyPrinter(indent, width,depth, stream, compact)
indent 参数定义在每个递归级别上添加的缩进。默认值为 1。
width 参数默认为 80。所需输出受此值限制。如果长度大于 width,则将其分成多行。
depth 参数控制要打印的级别数。
stream 参数默认为 std.out – 默认输出设备。它可以采用任何流对象,例如文件。
compact 参数 id 默认设置为 False。如果为 true,则仅显示在 width 范围内可调整的数据。
PrettyPrinter 类定义以下方法 −
pprint() −打印 PrettyPrinter 对象的格式化表示
pformat() − 根据构造函数的参数返回对象的格式化表示。
以下示例演示了 PrettyPrinter 类的简单使用。
import pprint students = {"Dilip":["English", "Maths", "Science"], "Raju":{"English":50,"Maths":60, "Science":70}, "Kalpana":(50,60,70)} pp = pprint.PrettyPrinter() print ("normal print output") print (students) print ("----") print ("pprint output") pp.pprint(students)
输出显示正常以及漂亮的打印显示。
normal print output {'Dilip': ['English', 'Maths', 'Science'], 'Raju': {'English': 50, 'Maths': 60, 'Science': 70}, 'Kalpana': (50, 60, 70)} ---- pprint 输出 {'Dilip': ['English', 'Maths', 'Science'], 'Kalpana': (50, 60, 70), 'Raju': {'English': 50, 'Maths': 60, 'Science': 70}}
pprint 模块还定义了与 PrettyPrinter 方法相对应的便捷函数 pprint() 和 pformat()。下面的示例使用了 pprint() 函数。
from pprint import pprint students = {"Dilip":["English", "Maths", "Science"], "Raju":{"English":50,"Maths":60, "Science":70}, "Kalpana":(50,60,70)} print ("normal print output") print (students) print ("----") print ("pprint output") pprint (students)
下一个示例使用 pformat() 方法以及 pformat() 函数。要使用 pformat() 方法,首先要设置 PrettyPrinter 对象。在这两种情况下,格式化的表示都是使用正常的 print() 函数显示的。
import pprint students = {"Dilip":["English", "Maths", "Science"], "Raju":{"English":50,"Maths":60, "Science":70}, "Kalpana":(50,60,70)} print ("使用 pformat 方法") pp = pprint.PrettyPrinter() string = pp.pformat(students) print (string) print ('------&39;) print ("使用 pformat 函数") string = pprint.pformat(students) print (字符串)
以下是上述代码的输出
使用 pformat 方法 {'Dilip': ['English', 'Maths', 'Science'], 'Kalpana': (50, 60, 70), 'Raju': {'English': 50, 'Maths': 60, 'Science': 70}} ------ 使用 pformat 函数 {'Dilip': ['English', 'Maths', 'Science'], 'Kalpana': (50, 60, 70), 'Raju': {'English': 50, 'Maths': 60, 'Science': 70}}
Pretty 打印机还可以与自定义类一起使用。类内部的 __repr__() 方法被重写。使用 repr() 函数时会调用 __repr__() 方法。它是 Python 对象的官方字符串表示。当我们使用对象作为 print() 函数的参数时,它会打印 repr() 函数的返回值。
在下面的例子中,__repr__() 方法返回 player 对象的字符串表示形式
import pprint class player: def __init__(self, name, forms = [], runs = []): self.name = name self.formats = forms self.runs = runs def __repr__(self): dct = {} dct[self.name] = dict(zip(self.formats,self.runs)) return (repr(dct)) l1 = ['Tests','ODI','T20'] l2 = [[140, 45, 39],[15,122,36,67, 100, 49],[78,44, 12, 0, 23, 75]] p1 = player("virat",l1,l2) pp = pprint.PrettyPrinter() pp.pprint(p1)
上述代码的输出为 −
{'virat': {'Tests': [140, 45, 39], 'ODI': [15, 122, 36, 67, 100, 49], 'T20': [78, 44, 12, 0, 23, 75]}}
带有 pprint 的递归数据结构
当我们尝试使用 pprint 打印递归对象,仅显示第一个表示,对于后续递归,仅打印其引用。
>>> import pprint >>> numbers = list(range(1,6)) >>> numbers.append(numbers) >>> print (numbers) [1, 2, 3, 4, 5, [...]] >>> pprint.pprint(numbers) [1, 2, 3, 4, 5, <Recursion on list with id=1403633698824>]
限制输出宽度
如果将宽度参数从默认的 80 更改为其他值,则输出的格式将设置为显示多行,同时注意不要违反语法。
import pprint students = {"Dilip":["English", "Maths", "Science"], "Raju":{"English":50,"Maths":60, "Science":70}, "Kalpana":(50,60,70)} pp=pprint.PrettyPrinter(width = 20) pp.pprint(students)
代码与本文中的第一个示例类似。但是,PrettyPrinter 对象构造时将宽度参数设为 20。因此,pprint 输出的格式也应如此。
{'Dilip': [ 'English', 'Maths', 'Science'], 'Kalpana': (50, 60, 70), 'Raju': {'English': 50, 'Maths': 60, 'Science': 70}}