美文网首页
18年第32周:pprint模块的用法

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

作者: 习惯研究所所长 | 来源:发表于2018-08-10 20:32 被阅读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

相关文章

网友评论

      本文标题:18年第32周:pprint模块的用法

      本文链接:https://www.haomeiwen.com/subject/blblbftx.html