python字符串检索值

python字符串检索值,python,python-3.x,Python,Python 3.x,有人能帮我从下面的字符串中获取键:'labelnumber'的值吗 尝试了json操作和评估,但没有成功 {'Value': 'infotech', 'Key': 'company'},{'Value': 'printing', 'Key': 'LineofBusiness'},{'Value': 'Non-Prod', 'Key': 'Environment'},{'Value': '1234-5623', 'Key': 'labelnumber'},{'Value': 'globalinfr

有人能帮我从下面的字符串中获取
键:'labelnumber'
值吗

尝试了json操作和评估,但没有成功

{'Value': 'infotech', 'Key': 'company'},{'Value': 'printing', 'Key': 'LineofBusiness'},{'Value': 'Non-Prod', 'Key': 'Environment'},{'Value': '1234-5623', 'Key': 'labelnumber'},{'Value': 'globalinfra', 'Key': 'ApplicationName'}
我需要得到“labelnumber”键的值,即“1234-5623”

这个字符串来自字典

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':'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'}
>>>印刷品(d1类)
>>>打印(d1[“值”])
{'Value':'infotech','Key':'company'},{'Value':'printing','Key':'LineofBusiness'},{'Value':'Non Prod','Key':'Environment'},{'Value':'1234-5623','Key':'labelnumber'},{'Value':'globalinfra','Key':'ApplicationName'}
输出:
1234-5623


使用eval()获取字典的元组,在单个字典中循环使用它,访问所需的密钥。

您显示的内容不是有效的JSON,因为它不使用双引号。
string1
是包含此值的变量吗?如果是这样的话,我怀疑这是一个列表,你能在你的问题中添加
print(type(string1))
的输出吗?不清楚你在这里显示了什么以及你尝试了什么。请提供一份清晰、完整的更新信息。。很抱歉造成混淆。所以,您这里没有JSON。。。正确的解决方案是确定如何创建
d1
(为什么第一个值必须是字符串?可以是列表吗?)。否则,您可以使用正则表达式分组来查找每个
{'Value':?,'Key':?}
对象(因为在逗号上拆分也不起作用),非常感谢您。。你的解决方案很有效。谢谢你的帮助!
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'])