跳转至

pprint 模块:打印 Python 对象

pprint 是 pretty printer 的缩写,用来打印 Python 数据结构,与 print 相比,它打印出来的结构更加整齐,便于阅读。

In [1]:

import pprint

生成一个 Python 对象:

In [2]:

data = (
    "this is a string", 
    [1, 2, 3, 4], 
    ("more tuples", 1.0, 2.3, 4.5), 
    "this is yet another string"
    )

使用普通的 print 函数:

In [3]:

print data
('this is a string', [1, 2, 3, 4], ('more tuples', 1.0, 2.3, 4.5), 'this is yet another string')

使用 pprint 模块中的 pprint 函数:

In [4]:

pprint.pprint(data)
('this is a string',
 [1, 2, 3, 4],
 ('more tuples', 1.0, 2.3, 4.5),
 'this is yet another string')

可以看到,这样打印出来的公式更加美观。


我们一直在努力

apachecn/AiLearning

【布客】中文翻译组