Can some one please help me how to get the Value
for Key: 'labelnumber'
from string below?
Tried json manipulation and eval but no luck.
{'Value': 'infotech', 'Key': 'company'},{'Value': 'printing', 'Key': 'LineofBusiness'},{'Value': 'Non-Prod', 'Key': 'Environment'},{'Value': '1234-5623', 'Key': 'labelnumber'},{'Value': 'globalinfra', 'Key': 'ApplicationName'}
I need to get the value of "labelnumber" key which is "1234-5623"
This string comes from a dictionary
d1 = {'Name': 'Maintags', 'Type': 'String', 'Value': "{'Value': 'infotech', 'Key': 'company'},{'Value': 'printing', 'Key': 'LineofBusiness'},{'Value': 'Non-Prod', 'Key': 'Environment'},{'Value': '1234-5623', 'Key': 'labelnumber'},{'Value': 'globalinfra', 'Key': 'ApplicationName'}"}
>>>print(d1)
{'Name': 'Maintags', 'Type': 'String', 'Value': "{'Value': 'infotech', 'Key': 'company'},{'Value': 'printing', 'Key': 'LineofBusiness'},{'Value': 'Non-Prod', 'Key': 'Environment'},{'Value': '1234-5623', 'Key': 'labelnumber'},{'Value': 'globalinfra', 'Key': 'ApplicationName'}"}
>>>print(type(d1))
<class 'dict'>
>>>print(d1["Value"])
{'Value': 'infotech', 'Key': 'company'},{'Value': 'printing', 'Key': 'LineofBusiness'},{'Value': 'Non-Prod', 'Key': 'Environment'},{'Value': '1234-5623', 'Key': 'labelnumber'},{'Value': 'globalinfra', 'Key': 'ApplicationName'}
d1 = {'Name': 'Maintags', 'Type': 'String', 'Value': "{'Value': 'infotech', 'Key': 'company'},{'Value': 'printing', 'Key': 'LineofBusiness'},{'Value': 'Non-Prod', 'Key': 'Environment'},{'Value': '1234-5623', 'Key': 'labelnumber'},{'Value': 'globalinfra', 'Key': 'ApplicationName'}"}
my_values_dict = {d['Key']:d['Value'] for d in eval(d1['Value'])}
print(my_values_dict['labelnumber'])
output: 1234-5623
Use eval() to get a tuple of dictionaries, loop through it to turn it in a single dictionary, access the key you need.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.