8

如何强制 pprint() 每行打印一个列表/元组/字典元素?

>>> from pprint import pprint
>>> my_var = ['one', 'two', ('red','green'), {'state' : 'Oregon', 'city' : 'Portland'}]
>>> pprint(my_var)
['one', 'two', ('red', 'green'), {'city': 'Portland', 'state': 'Oregon'}]

我希望输出类似于:

['one',
 'two',
 ('red',
  'green'),
 {'city': 'Portland',
  'state': 'Oregon'}]
4

1 回答 1

10

对 pprint() 使用 width=1 参数:

>>> from pprint import pprint
>>> my_var = ['one', 'two', ('red','green'), {'state' : 'Oregon', 'city' : 'Portland'}]
>>> pprint(my_var, width=1)
['one',
 'two',
 ('red',
  'green'),
 {'city': 'Portland',
  'state': 'Oregon'}]
>>>

“pprint - 漂亮的数据打印机” 文档

于 2013-03-21T14:42:42.607 回答