Python 3.x 从Python3字典对象获取标记。AWS Boto3 Python 3

Python 3.x 从Python3字典对象获取标记。AWS Boto3 Python 3,python-3.x,aws-lambda,boto3,Python 3.x,Aws Lambda,Boto3,我有一个从AWS返回给我的dictionary对象。我需要从这本字典中取出标签“based_on_ami”。我曾尝试转换为列表,但我对编程还不熟悉,无法理解如何访问标记,因为它们在字典中处于较低的级别 对于我来说,从字典中提取标签并将其放入我可以使用的变量中的最佳方法是什么 { “图像”:[ { “架构”:“x86_64”, “创作日期”:“2017-11-27T14:41:30.000Z”, 'ImageId':'ami-8e73e0f4', “图像位置”:“2345234545/java8

我有一个从AWS返回给我的dictionary对象。我需要从这本字典中取出标签“based_on_ami”。我曾尝试转换为列表,但我对编程还不熟悉,无法理解如何访问标记,因为它们在字典中处于较低的级别

对于我来说,从字典中提取标签并将其放入我可以使用的变量中的最佳方法是什么

{
“图像”:[
{
“架构”:“x86_64”,
“创作日期”:“2017-11-27T14:41:30.000Z”,
'ImageId':'ami-8e73e0f4',
“图像位置”:“2345234545/java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5”,
'ImageType':'machine',
"公众":假,,
'OwnerId':'2345234545',
“状态”:“可用”,
“BlockDeviceMappings”:[
{
“DeviceName”:“/dev/sda1”,
“Ebs”:{
“加密”:False,
“DeleteOnTermination”:True,
“快照ID”:“快照-0c10e8f5ced5b5240”,
“体积大小”:8,
“VolumeType”:“gp2”
}
},
{
“DeviceName”:“/dev/sdb”,
“虚拟名称”:“短暂的0”
},
{
“DeviceName”:“/dev/sdc”,
“虚拟名称”:“短暂的1”
}
],
“支持”:正确,
“虚拟机监控程序”:“xen”,
“名称”:“java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5”,
“RootDeviceName”:“/dev/sda1”,
'RootDeviceType':'ebs',
'SriovNetSupport':'simple',
“标签”:[
{
“键”:“服务”,
'Value':'baseami'
},
{
'Key':'cloudservice',
“值”:“ami”
},
{
'键':'名称',
“值”:“java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5”
},
{
“键”:“操作系统”,
“值”:“ubuntu 16.04 lts”
},
{
“Key”:“基于ami”,
“值”:“ami-aa2ea8d0”
}
],
“虚拟化类型”:“hvm”
}
],
“ResponseMetadata”:{
“请求ID”:“2c376c75-c31f-4aba-a058-173f3b125a00”,
“HTTPStatusCode”:200,
“HTTPHeaders”:{
“内容类型”:“text/xml;charset=UTF-8”,
“传输编码”:“分块”,
'vary':'Accept-Encoding',
“日期”:“2017年12月1日星期五18:17:53 GMT”,
“服务器”:“AmazonEC2”
},
“RetryAttempts”:0
}

}
解决这类问题的最佳方法是找到您正在寻找的价值,然后向外努力,直到找到解决方案。你需要看看每一个层次上都有什么

那么,你在找什么?您正在查找基于ami的
键的
值。因此,您的最后一步是:

if obj['Key'] == 'based_on_ami':
   # do something with obj['Value'].
但是你怎么去那里呢?对象位于列表中,因此需要迭代列表:

for tag in <some list>:
   if tag['Key'] == 'based_on_ami':
       # do something with tag['Value'].
那些标签在哪里?在列表中找到的图像对象中:

for image in image_list:
    for tag in image['Tags']:
       if tag['Key'] == 'based_on_ami':
           # do something with tag['Value'].
图像列表是在初始dict中的
Images
键处找到的值

image_list = my_data['Images']
for image in image_list:
    for tag in image['Tags']:
       if tag['Key'] == 'based_on_ami':
           # do something with tag['Value'].
现在您正在收集所有这些值,因此您需要一个
列表
,并需要附加到该列表中:

result = []
image_list = my_data['Images']
for image in image_list:
    for tag in image['Tags']:
       if tag['Key'] == 'based_on_ami':
           result.append(tag['Value'])
因此,我以上面的示例为例,添加了另一个基于ami的
节点,其值为
quack

{'ResponseMetadata': {'RequestId': '2c376c75-c31f-4aba-a058-173f3b125a00', 'RetryAttempts': 0, 'HTTPHeaders': {'vary': 'Accept-Encoding', 'transfer-encoding': 'chunked', 'server': 'AmazonEC2', 'content-type': 'text/xml;charset=UTF-8', 'date': 'Fri, 01 Dec 2017 18:17:53 GMT'}, 'HTTPStatusCode': 200}, 'Images': [{'Public': False, 'CreationDate': '2017-11-27T14:41:30.000Z', 'BlockDeviceMappings': [{'Ebs': {'SnapshotId': 'snap-0c10e8f5ced5b5240', 'VolumeSize': 8, 'Encrypted': False, 'VolumeType': 'gp2', 'DeleteOnTermination': True}, 'DeviceName': '/dev/sda1'}, {'VirtualName': 'ephemeral0', 'DeviceName': '/dev/sdb'}, {'VirtualName': 'ephemeral1', 'DeviceName': '/dev/sdc'}], 'OwnerId': '23452345234545', 'ImageLocation': '23452345234545/java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5', 'RootDeviceName': '/dev/sda1', 'ImageType': 'machine', 'Hypervisor': 'xen', 'RootDeviceType': 'ebs', 'State': 'available', 'Architecture': 'x86_64', 'Name': 'java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5', 'Tags': [{'Value': 'baseami', 'Key': 'service'}, {'Value': 'ami', 'Key': 'cloudservice'}, {'Value': 'java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5', 'Key': 'Name'}, {'Value': 'ubuntu 16.04 lts', 'Key': 'os'}, {'Value': 'ami-aa2ea8d0', 'Key': 'based_on_ami'}], 'EnaSupport': True, 'SriovNetSupport': 'simple', 'ImageId': 'ami-8e73e0f4'}, {'Public': False, 'CreationDate': '2017-11-27T14:41:30.000Z', 'BlockDeviceMappings': [{'Ebs': {'SnapshotId': 'snap-0c10e8f5ced5b5240', 'VolumeSize': 8, 'Encrypted': False, 'VolumeType': 'gp2', 'DeleteOnTermination': True}, 'DeviceName': '/dev/sda1'}, {'VirtualName': 'ephemeral0', 'DeviceName': '/dev/sdb'}, {'VirtualName': 'ephemeral1', 'DeviceName': '/dev/sdc'}], 'VirtualizationType': 'hvm', 'OwnerId': '23452345234545', 'ImageLocation': '23452345234545/java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5', 'RootDeviceName': '/dev/sda1', 'ImageType': 'machine', 'Hypervisor': 'xen', 'RootDeviceType': 'ebs', 'State': 'available', 'Architecture': 'x86_64', 'Name': 'java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5', 'Tags': [{'Value': 'baseami', 'Key': 'service'}, {'Value': 'ami', 'Key': 'cloudservice'}, {'Value': 'java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5', 'Key': 'Name'}, {'Value': 'ubuntu 16.04 lts', 'Key': 'os'}, {'Value': 'quack', 'Key': 'based_on_ami'}], 'EnaSupport': True, 'SriovNetSupport': 'simple', 'ImageId': 'ami-8e73e0f4'}]}
我的结果是:

['ami-aa2ea8d0', 'quack']

解决这类问题的最好方法是找到你想要的价值,然后向外努力,直到找到解决方案。你需要看看每一个层次上都有什么

那么,你在找什么?您正在查找基于ami的
键的
值。因此,您的最后一步是:

if obj['Key'] == 'based_on_ami':
   # do something with obj['Value'].
但是你怎么去那里呢?对象位于列表中,因此需要迭代列表:

for tag in <some list>:
   if tag['Key'] == 'based_on_ami':
       # do something with tag['Value'].
那些标签在哪里?在列表中找到的图像对象中:

for image in image_list:
    for tag in image['Tags']:
       if tag['Key'] == 'based_on_ami':
           # do something with tag['Value'].
图像列表是在初始dict中的
Images
键处找到的值

image_list = my_data['Images']
for image in image_list:
    for tag in image['Tags']:
       if tag['Key'] == 'based_on_ami':
           # do something with tag['Value'].
现在您正在收集所有这些值,因此您需要一个
列表
,并需要附加到该列表中:

result = []
image_list = my_data['Images']
for image in image_list:
    for tag in image['Tags']:
       if tag['Key'] == 'based_on_ami':
           result.append(tag['Value'])
因此,我以上面的示例为例,添加了另一个基于ami的
节点,其值为
quack

{'ResponseMetadata': {'RequestId': '2c376c75-c31f-4aba-a058-173f3b125a00', 'RetryAttempts': 0, 'HTTPHeaders': {'vary': 'Accept-Encoding', 'transfer-encoding': 'chunked', 'server': 'AmazonEC2', 'content-type': 'text/xml;charset=UTF-8', 'date': 'Fri, 01 Dec 2017 18:17:53 GMT'}, 'HTTPStatusCode': 200}, 'Images': [{'Public': False, 'CreationDate': '2017-11-27T14:41:30.000Z', 'BlockDeviceMappings': [{'Ebs': {'SnapshotId': 'snap-0c10e8f5ced5b5240', 'VolumeSize': 8, 'Encrypted': False, 'VolumeType': 'gp2', 'DeleteOnTermination': True}, 'DeviceName': '/dev/sda1'}, {'VirtualName': 'ephemeral0', 'DeviceName': '/dev/sdb'}, {'VirtualName': 'ephemeral1', 'DeviceName': '/dev/sdc'}], 'OwnerId': '23452345234545', 'ImageLocation': '23452345234545/java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5', 'RootDeviceName': '/dev/sda1', 'ImageType': 'machine', 'Hypervisor': 'xen', 'RootDeviceType': 'ebs', 'State': 'available', 'Architecture': 'x86_64', 'Name': 'java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5', 'Tags': [{'Value': 'baseami', 'Key': 'service'}, {'Value': 'ami', 'Key': 'cloudservice'}, {'Value': 'java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5', 'Key': 'Name'}, {'Value': 'ubuntu 16.04 lts', 'Key': 'os'}, {'Value': 'ami-aa2ea8d0', 'Key': 'based_on_ami'}], 'EnaSupport': True, 'SriovNetSupport': 'simple', 'ImageId': 'ami-8e73e0f4'}, {'Public': False, 'CreationDate': '2017-11-27T14:41:30.000Z', 'BlockDeviceMappings': [{'Ebs': {'SnapshotId': 'snap-0c10e8f5ced5b5240', 'VolumeSize': 8, 'Encrypted': False, 'VolumeType': 'gp2', 'DeleteOnTermination': True}, 'DeviceName': '/dev/sda1'}, {'VirtualName': 'ephemeral0', 'DeviceName': '/dev/sdb'}, {'VirtualName': 'ephemeral1', 'DeviceName': '/dev/sdc'}], 'VirtualizationType': 'hvm', 'OwnerId': '23452345234545', 'ImageLocation': '23452345234545/java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5', 'RootDeviceName': '/dev/sda1', 'ImageType': 'machine', 'Hypervisor': 'xen', 'RootDeviceType': 'ebs', 'State': 'available', 'Architecture': 'x86_64', 'Name': 'java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5', 'Tags': [{'Value': 'baseami', 'Key': 'service'}, {'Value': 'ami', 'Key': 'cloudservice'}, {'Value': 'java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5', 'Key': 'Name'}, {'Value': 'ubuntu 16.04 lts', 'Key': 'os'}, {'Value': 'quack', 'Key': 'based_on_ami'}], 'EnaSupport': True, 'SriovNetSupport': 'simple', 'ImageId': 'ami-8e73e0f4'}]}
我的结果是:

['ami-aa2ea8d0', 'quack']