Python之pprint 模块的使用
pprint 模块的作用
pprint模块包含 一个"格式化打印",用于生成数据结构的一个美观视图。格式化工具会生成数据结构的一些表示,不仅能够由解释器正确的地解析,便于人阅读。输出会尽可能放在一行上,分解为多行时会缩进。
1、准备演示打印的数据
data = [ (1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}), (2, {'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H', 'i': 'I', 'j': 'J', 'k': 'K', 'l': 'L'}), (3, ['m', 'n']), (4, ['o', 'p', 'q']), (5, ['r', 's', 't''u', 'v', 'x', 'y', 'z']), ]
2、pprint与print打印比较
from pprint import pprint from pprint_data import data print('PRINT:') print(data) print() print('PPRINT:') pprint(data)
打印效果
PRINT: [(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}), (2, {'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H', 'i': 'I', 'j': 'J', 'k': 'K', 'l': 'L'}), (3, ['m', 'n']), (4, ['o', 'p', 'q']),
(5, ['r', 's', 'tu', 'v', 'x', 'y', 'z'])] PPRINT: [(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}), (2, {'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H', 'i': 'I', 'j': 'J', 'k': 'K', 'l': 'L'}), (3, ['m', 'n']), (4, ['o', 'p', 'q']), (5, ['r', 's', 'tu', 'v', 'x', 'y', 'z'])]
3、格式化为字符串块,用于遍历打印
import logging from pprint import pformat from pprint_data import data logging.basicConfig( level=logging.DEBUG, format='%(levelname)-8s %(message)s', ) logging.debug('Logging pformatted data') formatted = pformat(data) for line in formatted.splitlines(): logging.debug(line.rstrip())
打印效果
DEBUG Logging pformatted data DEBUG [(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}), DEBUG (2, DEBUG {'e': 'E', DEBUG 'f': 'F', DEBUG 'g': 'G', DEBUG 'h': 'H', DEBUG 'i': 'I', DEBUG 'j': 'J', DEBUG 'k': 'K', DEBUG 'l': 'L'}), DEBUG (3, ['m', 'n']), DEBUG (4, ['o', 'p', 'q']), DEBUG (5, ['r', 's', 'tu', 'v', 'x', 'y', 'z'])]
4、类定义格式化打印内置方法:__repr__(),使用pprint将调用该方法显示
from pprint import pprint class node(object): def __init__(self, name, contents=[]): self.name = name self.contents = contents[:] def __repr__(self): return ( 'node(' + repr(self.name) + ', ' + repr(self.contents) + ')' ) trees = [ node('node-1'), node('node-2', [node('node-2-1')]), node('node-3', [node('node-3-1')]), ] pprint(trees)
打印效果
[node('node-1', []), node('node-2', [node('node-2-1', [])]), node('node-3', [node('node-3-1', [])])]
5、列表嵌入本身自已的内存对象,将生成一个递归的内存对象Recursion
from pprint import pprint local_data = ['a', 'b', 1, 2] local_data.append(local_data) print('id(local_data) =>', id(local_data)) pprint(local_data)
打印效果
id(local_data) => 2069064673416 ['a', 'b', 1, 2, <Recursion on list with id=2069064673416>]
6、限制嵌套输出
from pprint import pprint from pprint_data import data pprint(data, depth=1) pprint(data, depth=2)
打印效果
[(...), (...), (...), (...), (...)]
[(1, {...}), (2, {...}), (3, [...]), (4, [...]), (5, [...])]
7、设置输出控制台的宽度
from pprint import pprint from pprint_data import data for width in [80, 5]: print('WIDTH =', width) pprint(data, width=width) print()
打印效果
WIDTH = 80 [(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}), (2, {'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H', 'i': 'I', 'j': 'J', 'k': 'K', 'l': 'L'}), (3, ['m', 'n']), (4, ['o', 'p', 'q']), (5, ['r', 's', 'tu', 'v', 'x', 'y', 'z'])] WIDTH = 5 [(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}), (2, {'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H', 'i': 'I', 'j': 'J', 'k': 'K', 'l': 'L'}), (3, ['m', 'n']), (4, ['o', 'p', 'q']), (5, ['r', 's', 'tu', 'v', 'x', 'y', 'z'])]
8、compact标记告诉pprint()尝试在每一行上放置更多数据,而不是把复杂数据结构分解为多行
from pprint import pprint from pprint_data import data print('DEFAULT:') pprint(data, compact=False) print('\nCOMPACT:') pprint(data, compact=True)
打印效果
DEFAULT: [(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}), (2, {'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H', 'i': 'I', 'j': 'J', 'k': 'K', 'l': 'L'}), (3, ['m', 'n']), (4, ['o', 'p', 'q']), (5, ['r', 's', 'tu', 'v', 'x', 'y', 'z'])] COMPACT: [(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}), (2, {'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H', 'i': 'I', 'j': 'J', 'k': 'K', 'l': 'L'}), (3, ['m', 'n']), (4, ['o', 'p', 'q']), (5, ['r', 's', 'tu', 'v', 'x', 'y', 'z'])]