2017-04-06 79 views
0

我使用boto3,我跑了这个循环:无法访问数据字典中boto3

for i in x["Instances"] 
    print(i) 

然后我得到:

{ 
    'AmiLaunchIndex': 0, 
    'Hypervisor': 'xen', 
    'VpcId': 'vpc-a790ac1', 
    'Architecture': 'x86_64', 
    'InstanceId': 'i-0bab3fb8314', 
    'PrivateDnsName': 'ip-10-c2.internal', 
    'BlockDeviceMappings': [{ 
     'Ebs': { 
      'DeleteOnTermination': True, 
      'AttachTime': datetime.datetime(2017, 4, 4, 20, 44, 27, tzinfo = tzutc()), 
      'VolumeId': 'vol-07fd506f45', 
      'Status': 'attached' 
     }, 
     'DeviceName': '/dev/xvda' 
    }, { 
     'Ebs': { 
      'DeleteOnTermination': False, 
      'AttachTime': datetime.datetime(2017, 4, 6, 1, 12, 45, tzinfo = tzutc()), 
      'VolumeId': 'vol-01ef36c45', 
      'Status': 'attached' 
     }, 
     'DeviceName': '/dev/sdf' 
    }], 
    'RootDeviceName': '/dev/xvda', 
    'InstanceType': 't2.micro', 
    'EnaSupport': True, 
    'ClientToken': 'ODrMT1465413', 
    'EbsOptimized': False, 
    'SubnetId': 'subnet-fb1a4', 
    'Monitoring': { 
     'State': 'disabled' 
    }, 
    'PublicDnsName': '', 
    'StateTransitionReason': 'User initiated (2017-04-06 01:15:22 GMT)', 
    'PrivateIpAddress': '10.10.4.116', 
    'RootDeviceType': 'ebs', 
    'Tags': [{ 
     'Value': 'wp2', 
     'Key': 'Name' 
    }, { 
     'Value': 'true', 
     'Key': 'backup' 
    }], 
    'ImageId': 'ami-0976f01f', 
    'StateReason': { 
     'Code': 'Client.UserInitiadShutdown', 
     'Message': 'Client.UserInitiatedShutdown: User initiated shutdown' 
    }, 
    'KeyName': 'pair2', 
    'ProductCodes': [], 
    'State': { 
     'Name': 'stopped', 
     'Code': 80 
    }, 
    'LaunchTime': datetime.datetime(2017, 4, 6, 1, 13, 1, tzinfo = tzutc()), 
    'Placement': { 
     'AvailabilityZone': 'us-east-1b', 
     'GroupName': '', 
     'Tenancy': 'default' 
    }, 
    'SourceDestCheck': True, 
    'NetworkInterfaces': [{ 
     'Description': 'Primary network interface', 
     'PrivateIpAddress': '10.10.4.116', 
     'PrivateIpAddresses': [{ 
      'Primary': True, 
      'PrivateIpAddress': '10.10.4.116' 
     }], 
     'Status': 'in-use', 
     'SubnetId': 'subnet-ffbcba4', 
     'VpcId': 'vpc-a790a7c1', 
     'Attachment': { 
      'DeleteOnTermination': True, 
      'AttachTime': datetime.datetime(2017, 4, 4, 20, 44, 26, tzinfo = tzutc()), 
      'DeviceIndex': 0, 
      'AttachmentId': 'eni-attach-c8398', 
      'Status': 'attached' 
     }, 
     'Ipv6Addresses': [], 
     'OwnerId': '895548', 
     'MacAddress': '0e:31:4c4:b6', 
     'Groups': [{ 
      'GroupId': 'sg-26c59', 
      'GroupName': 'web-dmz' 
     }], 
     'NetworkInterfaceId': 'eni-5383', 
     'SourceDestCheck': True 
    }], 
    'SecurityGroups': [{ 
     'GroupId': 'sg-2cab59', 
     'GroupName': 'web-dmz' 
    }], 
    'VirtualizationType': 'hvm' 
} 

我试图访问“VolumeId “使用类似:

for x in ["BlockDeviceMappings"][0]["Ebs"]["VolumeId"]: 
    print(x) 

我得到TypeError: string indices must be integers

它看起来像'BlockDeviceMappings'开始作为一个列表与其中的字典,但我不能'VolumeId'。

我也试过:

for x in ["BlockDeviceMappings"][0]: 
    for k,v in ["Ebs"]: 
     print(v) 

我也得到:

ValueError: too many values to unpack (expected 2) 

而且我想:

for x in ["BlockDeviceMappings"][0]: 
    for v in ["Ebs"]: 
     print(v) 

它打印 'EBS' 几次。

难道有人请指点我正确的方向吗?

回答

0

要获得VolumeId请使用

print x["Instances"][0]["BlockDeviceMappings"][0]["Ebs"]["VolumeId"] 

你只是错过x或_。 由于[“BlockDeviceMappings”] [0]评估为“B”,您将收到错误消息。 所以你想获得“球体”从“B”

要获得所有卷:

for i in x["Instances"]: 
    for b in i["BlockDeviceMappings"] 
     print b["Ebs"]["VolumeId"] 

如果你有常从复杂sturctures这样的数据,尝试像github.com一些古怪的搜索库/ akesterson/dpath-python,它可以只使用关键字提取数据

+0

谢谢,但这让我第一卷。如果有更多,它跳过。这是主要的问题,我可能没有解释清楚。 – SO03112

+0

这样做。谢谢! – SO03112

+0

任何时候,遗憾的是,计算机会猜测你需要哪一个数组或字典,即使你在前一行中提到过它...让我们希望有一天,AI或语言设计者会提供一些更容易 – Serge