python - pretty-print 命名元组

标签 python namedtuple pprint

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

相关文章:

python - 有没有办法设置 pytest 来运行 webapp2 服务器以获取覆盖率报告?

python - Python 中的 "named tuples"是什么?

python - 为什么通过字段名访问 namedtuple 比访问类的成员变量慢?

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

python 3.x : alternative pprint implementation

python - 在 python 函数中,我是否需要预先分配一个在 if 下有条件出现的值?

python - 类型错误 : unsupported operand type(s) for +: 'NoneType' and 'float'

python - 使用 RevitPythonShell 覆盖事件 View 中的图形设置

Python:修改包含数组 View 的单个字典项会修改所有项

python - pprint 十六进制数