简体   繁体   中英

Extracting value from a nested dict

I am trying to extract a value from a nested dict, which looks like this:

response = 
    {'TransitGatewayRouteTables': [{'TransitGatewayRouteTableId': 'tgw-rtb-0461b603f87a09881',
                                            'TransitGatewayId': 'tgw-0d79045d0f874bfd4',
                                            'State': 'available', 
                                            'DefaultAssociationRouteTable': False, 
                                            'DefaultPropagationRouteTable': True, 
                                            'CreationTime': datetime.datetime(2020, 6, 18, 2, 32, 25,
        tzinfo=tzlocal()),
                                            'Tags': []}], 
             'ResponseMetadata': {'RequestId': '8d427b26-7735-4154-a7a3-ed45c83b5894',
                                  'HTTPStatusCode': 200, 
                                  'HTTPHeaders': {'x-amzn-requestid': '8d427b26-7735-4154-a7a3-ed45c83b5894',
                                                  'content-type': 'text/xml;charset=UTF-8', 
                                                  'transfer-encoding': 'chunked', 
                                                  'vary': 'accept-encoding', 
                                                  'date': 'Thu, 18 Jun 2020 15:54:47 GMT', 
                                                  'server': 'AmazonEC2'}, 
                                                  'RetryAttempts': 0}}

I am trying to extract the value "tgw-rtb-0461b603f87a09881" from this failing to do so. Have tried using

print (response['TransitGatewayRouteTables']['TransitGatewayRouteTableId'])

but that gives an error: "list indices must be integers or slices, not str: TypeError"

I am able to get one level deeper with the following:

rtid = response.values()
print(rtid)

This gets me to the following

dict_values([[{'TransitGatewayRouteTableId': 'tgw-rtb-0461b603f87a09881', 'TransitGatewayId': 'tgw-0d79045d0f874bfd4', 'State': 'available', 'DefaultAssociationRouteTable': False, 'DefaultPropagationRouteTable': True, 'CreationTime': datetime.datetime(2020, 6, 18, 2, 32, 25, tzinfo=tzlocal()), 'Tags': []}], {'RequestId': '6a0ec6df-c41c-4e06-b98d-1afff74e5915', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '6a0ec6df-c41c-4e06-b98d-1afff74e5915', 'content-type': 'text/xml;charset=UTF-8', 'transfer-encoding': 'chunked', 'vary': 'accept-encoding', 'date': 'Thu, 18 Jun 2020 16:13:00 GMT', 'server': 'AmazonEC2'}, 'RetryAttempts': 0}])

Not quite sure if I am going in the right direction. Would be great if someone can help describe how to get the required value extracted.

In your case of nested dictionaries, you have to use the index [0] to access the value of outer dictionary which is a list and then the key to get the value from the inner dictionary

print (response['TransitGatewayRouteTables'][0]['TransitGatewayRouteTableId'])
# tgw-rtb-0461b603f87a09881

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM