打印namedtuple
:时出现问题
Info = namedtuple('Info', ['type', 'value', 'x', 'y'])
以便值对齐,并在它们之间留有空格(填充),例如:
Info( type='AAA', value=80000, x=16.4, y=164.2 )
Info( type='A', value=78, x=1.5, y=11.3 )
Info( type='ABBCD', value=554, x=11.7, y=10.1 )
Info( type='AFFG', value=1263, x=121.3, y=12.8 )
理想情况下,不要使用逗号。我尝试过pprint
,也尝试过使用_asdict
打印,但没有成功。和format
一样,我无法使它与命名元组一起工作。有什么想法或代码示例吗?
以下是我为命名元组实现的漂亮打印:
def prettyprint_namedtuple(namedtuple,field_spaces):
assert len(field_spaces) == len(namedtuple._fields)
string = "{0.__name__}( ".format(type(namedtuple))
for f_n,f_v,f_s in zip(namedtuple._fields,namedtuple,field_spaces):
string+= "{f_n}={f_v!r:<{f_s}}".format(f_n=f_n,f_v=f_v,f_s=f_s)
return string+")"
给出了我认为您正在寻找的输出:
a = Info( type='AAA', value=80000, x=16.4, y=164.2 )
b = Info( type='A', value=78, x=1.5, y=11.3 )
c = Info( type='ABBCD', value=554, x=11.7, y=10.1 )
d = Info( type='AFFG', value=1263, x=121.3, y=12.8 )
tups = [a,b,c,d]
for t in tups:
print(prettyprint_namedtuple(t,(10, 9, 8, 6)))
输出:
Info( type='AAA' value=80000 x=16.4 y=164.2 )
Info( type='A' value=78 x=1.5 y=11.3 )
Info( type='ABBCD' value=554 x=11.7 y=10.1 )
Info( type='AFFG' value=1263 x=121.3 y=12.8 )
因为您需要提前知道字段宽度,所以我能为您建议的唯一合理的解决方案就是编写一个辅助函数来转换为您想要的格式。
def namedtuple_to_str(t, field_widths=15):
if isinstance(field_widths, int):
field_widths = [field_widths] * len(t)
field_pairs = ['{}={}'.format(field, getattr(t, field)) for field in t._fields]
s = ' '.join('{{:{}}}'.format(w).format(f) for w,f in zip(field_widths, field_pairs))
result = '{}( {} )'.format(type(t).__name__, s)
return result
演示:
>>> from collections import namedtuple
>>> Info = namedtuple('Info', ['type', 'value', 'x', 'y'])
>>> t = Info(type='AAA', value=80000, x=16.4, y=164.2)
>>>
>>> print namedtuple_to_str(t)
Info( type=AAA value=80000 x=16.4 y=164.2 )
>>> print namedtuple_to_str(t, field_widths=11)
Info( type=AAA value=80000 x=16.4 y=164.2 )
>>> print namedtuple_to_str(t, field_widths=[10, 20, 7, 7])
Info( type=AAA value=80000 x=16.4 y=164.2 )
要打印这些字段的集合,使用max(..., key=len)
预先计算所需的字段宽度并不困难。