我尝试了pprint
中的pprint
,但是它的输出只有一行,没有多行输出,也没有缩进。
最佳答案
我使用namedtuple 的_asdict
方法。
但是,它返回一个 OrderedDict
,其中 pprint
不会缩进,所以我将它转换为 dict
:
>>> from collections import namedtuple
>>> Busbar = namedtuple('Busbar', 'id name voltage')
>>> busbar = Busbar(id=102, name='FACTORY', voltage=21.8)
使用 pprint
和 dict
:
>>> from pprint import pprint
>>> pprint(dict(busbar._asdict()))
{'id': 102,
'name': 'FACTORY',
'voltage': 21.8}
关于python - pretty-print 命名元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30062384/