我正在尝试从下面的输出中获取特定值。我试图获得" package_path"的价值。我试图找到最好的方法来做到这一点。我试过iteritems()
,但它似乎没有提供正确的值输出。我还有另一种方式吗?...谢谢..
[{"name": "ZONE", "value": "zone01"},
{"name": "VPCNetworkCIDRs", "value": "192.168.30.0/24"},
{"name": "DEPLOYMENT_ENVIRONMENT", "value": "AWS"},
{"name": "parameters_json", "value": "s3://my_folder/zone01/zone01.json"},
{"name": "REGION", "value": "us-east-1"},
{"name": "SECTOR", "value": ""},
{"name": "package_path", "value": "s3://my_bucket/packages/my_app.1.0.2.0.pkg"},
{"name": "component_type", "value": "WebApplication"},
{"name": "PROFILE", "value": "dev"}]
答案 0 :(得分:2)
由于你拥有的是词典列表,你必须遍历他们的name
属性才能找到你想要的词典。
my_list = [{"name": "ZONE", "value": "zone01"}, {"name": "VPCNetworkCIDRs", "value": "192.168.30.0/24"},
{"name": "DEPLOYMENT_ENVIRONMENT", "value": "AWS"},
{"name": "parameters_json", "value": "s3://my_folder/zone01/zone01.json"}, {"name": "REGION", "value": "us-east-1"},
{"name": "SECTOR", "value": ""}, {"name": "package_path", "value": "s3://my_bucket/packages/my_app.1.0.2.0.pkg"},
{"name": "component_type", "value": "WebApplication"}, {"name": "PROFILE", "value": "dev"}]
package_path = None
# For each name/value pair in my_list...
for name_value in my_list:
# If the name attribute is package_path, we found it
if name_value['name'] == 'package_path':
package_path = name_value['value'] # Store our variable
break # Exit the loop
if package_path is None:
print('Package path not found :(')
else:
print(package_path) # s3://my_bucket/packages/my_app.1.0.2.0.pkg
答案 1 :(得分:2)
考虑将数据结构重新格式化为单个字典。例如,使用:
data = dict((item['name'], item['value']) for item in items)
然后:
print(data['package_path'])
将为您提供所需内容,以及其他类似内容。
答案 2 :(得分:2)
您可以使用列表理解。
[arg['value'] for arg in my_list if arg['name'] == 'package_path']