我想用 python 将 nametuple 转换为 dict: 我有:
CommentInfo(stt=1, gid=12, uid=222)
现在我想要:
{"stt":1,"gid":12,"uid":222}
请帮帮我!非常感谢!
最佳答案
您需要使用_asdict()
函数将命名元组转换为字典。
示例:
>>> CommentInfo = namedtuple('CommentInfo', ["stt", "gid", "uid"])
>>> x = CommentInfo(stt=1,gid=12,uid=222)
>>> x._asdict()
OrderedDict([('stt', 1), ('gid', 12), ('uid', 222)])
关于python - 如何在 python 中将 namedtuple 转换为 dict,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28911296/