想要仅从括号前后的value中删除单引号。我不想让它打结。由于这个单引号,这个json字典无效。
{
'NetworkInterfaces': '[{
"AssociatePublicIpAddress":true,
"DeleteOnTermination":true,
"Description":"Primary network interface",
"DeviceIndex":0,"Groups":["sg-0b60a9f7e75aba4d4"],
"Ipv6AddressCount":0,
"Ipv6Addresses":[],
"NetworkInterfaceId":"eni-0538f01e55dda59e8",
"PrivateIpAddress":"172.16.18.79",
"PrivateIpAddresses":[{"Primary":true,"PrivateIpAddress":"172.16.18.79"}],
"SecondaryPrivateIpAddressCount":0,
"SubnetId":"subnet-0b2e2d39e5c1a8af9",
"InterfaceType":"interface"
}]'
}
你得到的是一个JSON值的字典。您可以简单地将JSON转换为Python对象后重新赋值:
import json, pprint
d = { 'NetworkInterfaces': '[{ "AssociatePublicIpAddress":true, "DeleteOnTermination":true, "Description":"Primary network interface", "DeviceIndex":0,"Groups":["sg-0b60a9f7e75aba4d4"], "Ipv6AddressCount":0, "Ipv6Addresses":[], "NetworkInterfaceId":"eni-0538f01e55dda59e8", "PrivateIpAddress":"172.16.18.79", "PrivateIpAddresses":[{"Primary":true,"PrivateIpAddress":"172.16.18.79"}], "SecondaryPrivateIpAddressCount":0, "SubnetId":"subnet-0b2e2d39e5c1a8af9", "InterfaceType":"interface" }]' }
d['NetworkInterfaces'] = json.loads(d['NetworkInterfaces'])
pprint.pprint(d)
输出:
{'NetworkInterfaces': [{'AssociatePublicIpAddress': True,
'DeleteOnTermination': True,
'Description': 'Primary network interface',
'DeviceIndex': 0,
'Groups': ['sg-0b60a9f7e75aba4d4'],
'InterfaceType': 'interface',
'Ipv6AddressCount': 0,
'Ipv6Addresses': [],
'NetworkInterfaceId': 'eni-0538f01e55dda59e8',
'PrivateIpAddress': '172.16.18.79',
'PrivateIpAddresses': [{'Primary': True,
'PrivateIpAddress': '172.16.18.79'}],
'SecondaryPrivateIpAddressCount': 0,
'SubnetId': 'subnet-0b2e2d39e5c1a8af9'}]}