Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Can';t访问boto3中的字典数据_Python_Dictionary - Fatal编程技术网

Python Can';t访问boto3中的字典数据

Python Can';t访问boto3中的字典数据,python,dictionary,Python,Dictionary,我使用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',

我使用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:字符串索引必须是整数

“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”


有人能告诉我正确的方向吗?

要获取VolumeId,请使用

print x["Instances"][0]["BlockDeviceMappings"][0]["Ebs"]["VolumeId"]
你刚刚错过了x或ux。 您将收到一个错误,因为[“BlockDeviceMappings”][0]评估为“B”。 所以你想从“B”中得到“orb”

要获取所有卷,请执行以下操作:

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

如果您必须经常从复杂的结构中提取数据,请尝试一些古怪的搜索库,如github.com/akesterson/dpath-python,它只需使用关键字即可提取数据

谢谢,但这是第一卷。如果还有更多的话,它就跳过了。这是主要的问题,我可能没有解释清楚,就是这样。谢谢任何时候,遗憾的是,即使你们在前一行提到,计算机也会猜出你们需要哪个数组或字典。。。让我们期待有一天AI或语言设计师能提供更简单的东西,比如