【问题标题】:How to format my Python dictionary output? [duplicate]如何格式化我的 Python 字典输出? [复制]
【发布时间】:2020-11-17 18:05:43
【问题描述】:

这一行

for i,k in (p[0].__dict__).items():
    print (i,type(k))

打印

code_event <class 'str'>
code_event_system <class 'str'>
event_no <class 'int'>
group_no <class 'int'>

我想要的输出(对于 collections.nametuple0

code_event str
code_event str

如何解决这个问题?

【问题讨论】:

  • 您已经知道__dict__。你知道任何其他双下划线属性吗?

标签: python python-3.x


【解决方案1】:

使用__name__

for i,k in (p[0].__dict__).items():
    print (i,type(k).__name__)
>>> a = "hi"
>>> type(a).__name__
'str'
>>>

【讨论】:

  • 正是我想要的。
猜你喜欢
  • 2015-08-20
  • 1970-01-01
  • 2016-07-01
  • 2012-01-05
  • 2017-05-14
  • 2010-10-30
  • 1970-01-01
  • 1970-01-01
  • 2018-01-10
相关资源
最近更新 更多