简体   繁体   中英

Extracting Dictionary Values from a Python Tuple

I have a tuple r[0] which is of this format:

(OrderedDict([('attributes', OrderedDict([('type', 'CCE__c'), ('url', 'aA1')])), ('VARIABLE1', '00AE'), ('Opportunity__r', OrderedDict([('attributes', OrderedDict([('type', 'Opportunity'), ('url', 'NyzIAE')])), ('VARIABLE2', 'uJeIAK')])), ('VARIABLE3', 'a05EA1'))

I'm trying to extract VARIABLE1 and VARIABLE2. When I use:

r[0]['VARIABLE1']

I'm able to extract correctly. However, when I use:

r[0]['VARIABLE2']

it's throwing an error. Could someone tell me how to correctly extract Variable 2?

you have a small structural problem, to access the VARIABLE2 key, you must first access the Opportunity__r key.

Use the method of your variable items() , to see all keys:

r = (OrderedDict([('attributes', OrderedDict([('type', 'CCE__c'), ('url', 'aA1')])), ('VARIABLE1', '00AE'), ('Opportunity__r', OrderedDict([('attributes', OrderedDict([('type', 'Opportunity'), ('url', 'NyzIAE')])), ('VARIABLE2', 'uJeIAK')])), ('VARIABLE3', 'a05EA1')]), )
r[0].keys()
r[0]['Opportunity__r']['VARIABLE2']

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