gpt4 book ai didi

python - pretty-print 命名元组

转载 作者:IT老高 更新时间:2023-10-28 20:34:10 26 4
gpt4 key购买 nike

我尝试了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)

使用 pprintdict:

>>> 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com