简体   繁体   English

如何获取此函数的输出并将其用作字典?

[英]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.

相关问题 如何获取函数的输出并将其用作另一个函数python的输入? - How to take output of function and use as input of another function python? 如何从迭代中获取输出,并将其存储在字典中 - How to take output from iterating, store that in a dictionary 如何在“ for”循环中获取函数的输出并使用它构建数据框? - How to take output of function inside of 'for' loop and use it build dataframe? 我如何从一个 function 中取出字典并在另一个 python 中使用它? - How do I take a dictionary from one function and use it in another in python? 如何获取函数的打印输出并将其放入DataFrame - How to take printed output of a function and put it in a DataFrame 如何使用字典词典作为Python的输出? - How to use a dictionary of dictionaries as output from Python? 如何使用字典通过要求的输入输出字典键 - How to use a dictionary to output a dictionary key using an asked input 如何取一个字符串并将其用作标识符来调用函数? - How to take a string and use it as an identifier to call a function? 一种将字典作为输入并返回子字典作为输出的Python方法 - A pythonic way to take a dictionary as input and return a sub-dictionary as output 如何获取函数的输出并放入pandas数据框以进行进一步分析? - How to take the output of a function and put into pandas dataframe for further analysis?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM