简体   繁体   English

从Python元组中提取字典值

[英]Extracting Dictionary Values from a Python Tuple

I have a tuple r[0] which is of this format: 我有一个元组r [0],其格式如下:

(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. 我正在尝试提取VARIABLE1和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? 有人可以告诉我如何正确提取变量2吗?

you have a small structural problem, to access the VARIABLE2 key, you must first access the Opportunity__r key. 您有一个小的结构性问题,要访问VARIABLE2键,必须首先访问Opportunity__r键。

Use the method of your variable items() , to see all keys: 使用您的可变items() ,查看所有键:

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']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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