18年第32周:pprint模块的用法

一句话:使用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

你可能感兴趣的:(18年第32周:pprint模块的用法)