[英]How to take output of this function and use it as a dictionary?
code 码
data = 'address'
print(data.id)
when i run this code it will print the following output 当我运行此代码时,它将打印以下输出
(('instance', u'id 123456789'),)
how to make this output into a dictionary like this 如何使输出变成这样的字典
{"id":"123456789"}
Try with this 试试这个
k, v = (('instance', u'id 123456789'),)[0][1].split()
d = {str(k) : str(v)}
print(d)
I should output this 我应该输出这个
{'id': '123456789'}
To have them as unoce strings take the str(..) out. 要将它们作为unoce字符串取出str(..)。 (in python 3.x unicode is the same as str
(在python 3.x中unicode与str相同
d = dict([data.id[0][1].split()])
在此实例中(('instance', u'id 123456789'),)
data.id
为: (('instance', u'id 123456789'),)
。
>>> out = (('instance', u'id 123456789'),)
>>> a,b = list(out).pop()
>>> dict([str(b).split()])
{'id': '123456789'}
You can do something like... 您可以做类似...
res={} #initialize dictionary
res['id']=id(data)
print(res)
Result : 结果:
{'id': 1754862887072}
{'id':1754862887072}
Other than this... 除了这个...
print(data.id) # will throw error, str does not have id property
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.