18年第32周:pprint模块的用法
2018-08-10 本文已影响0人
习惯研究所所长
一句话:使用pprint输出更加美观
方式1:不使用pprint的结果
案例1:嵌套元组
tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
('parrot', ('fresh fruit',))))))))
print(tup)
('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', ('parrot', ('fresh fruit',))))))))
案例2:嵌套列表
stuff = ['a' * 10, tup, ['a' * 30, 'b' * 30], ['c' * 20, 'd' * 20]]
print(stuff)
['aaaaaaaaaa', ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', ('parrot', ('fresh fruit',)))))))), ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'], ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
方式2:使用pprint的结果
import pprint
案例1:嵌套元组
stuff = ['a' * 10, tup, ['a' * 30, 'b' * 30], ['c' * 20, 'd' * 20]]
pprint.pprint(stuff)
['aaaaaaaaaa',
('spam',
('eggs',
('lumberjack',
('knights', ('ni', ('dead', ('parrot', ('fresh fruit',)))))))),
['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
['cccccccccccccccccccc', 'dddddddddddddddddddd']]
案例2:嵌套列表
tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
('parrot', ('fresh fruit',))))))))
pprint.pprint(tup)
('spam',
('eggs',
('lumberjack', ('knights', ('ni', ('dead', ('parrot', ('fresh fruit',))))))))
参考链接:https://docs.python.org/2/library/pprint.html#pprint-example