Python 访问字典响应的值

Python 访问字典响应的值,python,python-2.7,amazon-web-services,boto,Python,Python 2.7,Amazon Web Services,Boto,尝试从boto3返回的数据中获取值时出错。我可以打印整个响应(见下文),但不知道需要做些什么才能使NetworkInterfaceId脱离响应 我在Python2.7.5中运行它,因为在默认情况下,需要运行它的实例就是这样的。我是python新手,希望我遗漏了一些简单的东西,谢谢你的帮助 错误 TypeError: list indices must be integers, not str 代码 #!/usr/bin/python import boto3 ec2 = boto3.clie

尝试从boto3返回的数据中获取值时出错。我可以打印整个响应(见下文),但不知道需要做些什么才能使NetworkInterfaceId脱离响应

我在Python2.7.5中运行它,因为在默认情况下,需要运行它的实例就是这样的。我是python新手,希望我遗漏了一些简单的东西,谢谢你的帮助

错误

TypeError: list indices must be integers, not str
代码

#!/usr/bin/python
import boto3

ec2 = boto3.client('ec2')
response = ec2.describe_route_tables(
              RouteTableIds=[
                  "rtb-4a1efc23",
              ],
              Filters=[
                  {
                      'Name': 'route.destination-cidr-block',
                      'Values': [
                          "172.29.0.0/16",
                      ]
                  },
              ]
          )

#print(response)
print(response["RouteTables"][0]["Routes"]["NetworkInterfaceId"])
 {'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': 'a8e7ba60-7599-450a-a708-8d90e429d59e', 'HTTPHeaders': {'transfer-encoding': 'chunked', 'vary': 'Accept-Encoding', 'server': 'AmazonEC2', 'content-type': 'text/xml;charset=UTF-8', 'date': 'Wed, 08 Feb 2017 11:51:47 GMT'}}, u'RouteTables': [{u'Associations': [{u'SubnetId': 'subnet-d7040aaf', u'RouteTableAssociationId': 'rtbassoc-867a94ef', u'Main': False, u'RouteTableId': 'rtb-4a1efc23'}, {u'SubnetId': 'subnet-e0fcd3aa', u'RouteTableAssociationId': 'rtbassoc-9f7a94f6', u'Main': False, u'RouteTableId': 'rtb-4a1efc23'}], u'RouteTableId': 'rtb-4a1efc23', u'VpcId': 'vpc-0d00e264', u'PropagatingVgws': [{u'GatewayId': 'vgw-fcf479cc'}], u'Tags': [{u'Value': 'pub', u'Key': 'Name'}], u'Routes': [{u'GatewayId': 'local', u'DestinationCidrBlock': '172.28.0.0/16', u'State': 'active', u'Origin': 'CreateRouteTable'}, {u'Origin': 'CreateRoute', u'DestinationCidrBlock': '172.29.0.0/16', u'InstanceId': 'i-0b84e502d9dc49443', u'NetworkInterfaceId': 'eni-08f55373', u'State': 'active', u'InstanceOwnerId': '444456106883'}, {u'Origin': 'CreateRoute', u'DestinationCidrBlock': '172.31.0.0/16', u'InstanceId': 'i-0b84e502d9dc49443', u'NetworkInterfaceId': 'eni-08f55373', u'State': 'active', u'InstanceOwnerId': '444456106883'}, {u'GatewayId': 'igw-7b03e012', u'DestinationCidrBlock': '0.0.0.0/0', u'State': 'active', u'Origin': 'CreateRoute'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.114.112.192/27', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.114.210.160/27', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.138.172.32/27', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.138.172.96/27', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.114.105.128/26', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.115.80.0/26', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.115.131.0/26', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.138.17.128/26', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.138.83.64/26', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.138.180.128/26', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.192.0.0/16', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}]}]} 
响应

#!/usr/bin/python
import boto3

ec2 = boto3.client('ec2')
response = ec2.describe_route_tables(
              RouteTableIds=[
                  "rtb-4a1efc23",
              ],
              Filters=[
                  {
                      'Name': 'route.destination-cidr-block',
                      'Values': [
                          "172.29.0.0/16",
                      ]
                  },
              ]
          )

#print(response)
print(response["RouteTables"][0]["Routes"]["NetworkInterfaceId"])
 {'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': 'a8e7ba60-7599-450a-a708-8d90e429d59e', 'HTTPHeaders': {'transfer-encoding': 'chunked', 'vary': 'Accept-Encoding', 'server': 'AmazonEC2', 'content-type': 'text/xml;charset=UTF-8', 'date': 'Wed, 08 Feb 2017 11:51:47 GMT'}}, u'RouteTables': [{u'Associations': [{u'SubnetId': 'subnet-d7040aaf', u'RouteTableAssociationId': 'rtbassoc-867a94ef', u'Main': False, u'RouteTableId': 'rtb-4a1efc23'}, {u'SubnetId': 'subnet-e0fcd3aa', u'RouteTableAssociationId': 'rtbassoc-9f7a94f6', u'Main': False, u'RouteTableId': 'rtb-4a1efc23'}], u'RouteTableId': 'rtb-4a1efc23', u'VpcId': 'vpc-0d00e264', u'PropagatingVgws': [{u'GatewayId': 'vgw-fcf479cc'}], u'Tags': [{u'Value': 'pub', u'Key': 'Name'}], u'Routes': [{u'GatewayId': 'local', u'DestinationCidrBlock': '172.28.0.0/16', u'State': 'active', u'Origin': 'CreateRouteTable'}, {u'Origin': 'CreateRoute', u'DestinationCidrBlock': '172.29.0.0/16', u'InstanceId': 'i-0b84e502d9dc49443', u'NetworkInterfaceId': 'eni-08f55373', u'State': 'active', u'InstanceOwnerId': '444456106883'}, {u'Origin': 'CreateRoute', u'DestinationCidrBlock': '172.31.0.0/16', u'InstanceId': 'i-0b84e502d9dc49443', u'NetworkInterfaceId': 'eni-08f55373', u'State': 'active', u'InstanceOwnerId': '444456106883'}, {u'GatewayId': 'igw-7b03e012', u'DestinationCidrBlock': '0.0.0.0/0', u'State': 'active', u'Origin': 'CreateRoute'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.114.112.192/27', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.114.210.160/27', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.138.172.32/27', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.138.172.96/27', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.114.105.128/26', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.115.80.0/26', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.115.131.0/26', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.138.17.128/26', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.138.83.64/26', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.138.180.128/26', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.192.0.0/16', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}]}]} 
从中,您可以发现
路由
是一个列表。如果要获取
NetworkInterfaceId
,则应循环通过
路由

for route in response["RouteTables"][0]['Routes']:
    if 'NetworkInterfaceId' in route:
        print route['NetworkInterfaceId']
请注意,
NetworkInterfaceId
可能在响应中存在,也可能不存在。我从您粘贴在此处的响应中得出了这一结论。

响应[“RouteTables”][0][“Routes”]
是一个列表。如果要获取
NetworkInterfaceId
,应尝试
response[“RouteTables”][0]['Routes'][0]['NetworkInterfaceId']