[英]How to convert nametuple to dict in python
I want convert nametuple to dict with python: I have: 我想转换nametuple与python dict:我有:
CommentInfo(stt=1, gid=12, uid=222)
Now I want: 现在我想:
{"stt":1,"gid":12,"uid":222}
Please help me! 请帮我! Thanks very much! 非常感谢!
You need to use _asdict()
function to convert the named tuples into a dictionary. 您需要使用_asdict()
函数将命名元组转换为字典。
Example: 例:
>>> CommentInfo = namedtuple('CommentInfo', ["stt", "gid", "uid"])
>>> x = CommentInfo(stt=1,gid=12,uid=222)
>>> x._asdict()
OrderedDict([('stt', 1), ('gid', 12), ('uid', 222)])
._asdict()
有一个._asdict()
方法将它转换为OrderedDict,所以如果你在变量comment
有一个实例,你可以使用comment._asdict()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.