2017-04-06 63 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