pprint 模块:打印 Python 对象¶

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

In [1]:

  1. import pprint

生成一个 Python 对象:

In [2]:

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

使用普通的 print 函数:

In [3]:

  1. print data
  1. ('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]:

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

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

原文: https://nbviewer.jupyter.org/github/lijin-THU/notes-python/blob/master/11-useful-tools/11.01-pprint.ipynb