问题:无法使用 key key 返回 d 的项目。引发 KeyError。
我正在尝试访问 CASE 字典。
资料:
od['PQ'] = \
OrderedDict({'@N':"E90", '@RT':"TG", 'CASES':
OrderedDict({'ASOF_DATE':"8/11/2017", 'CREATE_DATE':"8/13/2017", 'RECORDS':"1130", 'CASE':
OrderedDict({'ID':'E90'})})})
print(od['PQ'])
>>> OrderedDict([('@N', 'E90'), ('@RT', 'TG'), ('CASES', OrderedDict([('ASOF_DATE', '8/11/2017'), ('CREATE_DATE', '8/13/2017'), ('RECORDS', '1130'), ('CASE', OrderedDict([('ID', 'E90')]))]))])
print(od['PQ']['CASES'])
>>> OrderedDict([('ASOF_DATE', '8/11/2017'), ('CREATE_DATE', '8/13/2017'), ('RECORDS', '1130'), ('CASE', OrderedDict([('ID', 'E90')]))])
print(od['PQ']['CASES']['CASE'])
>>> OrderedDict([('ID', 'E90')])
print(od.get('PQ').get('CASES').get('CASE'))
>>> OrderedDict([('ID', 'E90')])
print('list(od[\'PQ\'])[2] {}'.format(list(od['PQ'].values())[2]))
>>> list(od['PQ'])[2] OrderedDict([('ASOF_DATE', '8/11/2017'), ('CREATE_DATE', '8/13/2017'), ('RECORDS', '1130'), ('CASE', OrderedDict([('ID', 'E90')]))])
for kv in list(od['PQ'].values()):
print('od[\'PQ\'].values {}'.format(kv))
>>> od['PQ'].values E90
>>> od['PQ'].values TG
>>> od['PQ'].values OrderedDict([('ASOF_DATE', '8/11/2017'), ('CREATE_DATE', '8/13/2017'), ('RECORDS', '1130'), ('CASE', OrderedDict([('ID', 'E90')]))])
for k,v in od.items():
print('items(od) {}:{}'.format(k,v))
>>> items(od) PQ:OrderedDict([('@N', 'E90'), ('@RT', 'TG'), ('CASES', OrderedDict([('ASOF_DATE', '8/11/2017'), ('CREATE_DATE', '8/13/2017'), ('RECORDS', '1130'), ('CASE', OrderedDict([('ID', 'E90')]))]))])
用 Python 测试:3.4.2