0

Accessing xmltodict converted values. I have an xml that looks like this

<PQ N="E90" RT="TG">
<CASES ASOF_DATE="8/11/2017" CREATE_DATE="8/13/2017" RECORDS="1130" >
<CASE>
<ID>E90</ID>

I am trying to access the CASE dictionary. If I remove the second XML line and try returning d[PQ]['CASE'], I get the desire result Here is the code for that:

def convert(xml_file, xml_attribs=True):
    with open(xml_file, "rb") as f:
        d = xmltodict.parse(f, xml_attribs=xml_attribs)
    return (d['FUND'])

This is how the output of d looks like:

OrderedDict([(PQ, OrderedDict([('@N', 'E90'), ('@RT', 'TG'), (CASES,   
OrderedDict([('@ASOF_DATE', '8/11/2017'), ('@CREATE_DATE', '8/13/2017'),  

('@RECORDS', '1130'), (CASE, [OrderedDict([('ID, E90), ..... so on
stovfl
  • 14,998
  • 7
  • 24
  • 51
J.D
  • 45
  • 1
  • 6
  • `CASE`is inside `CASES`, therefore you need `d[PQ]['CASES']['CASE'] – stovfl Aug 17 '17 at 12:12
  • It doesn't go like that. Since it creates an ordered dictionary, it doesn't allow d[PQ]['CASES']['CASE']. I can share the output when I return d. That might be helpful – J.D Aug 17 '17 at 12:28
  • This is how it looks like. I need to access the CASE ordered dictionary only. – J.D Aug 17 '17 at 12:54
  • @stovfl. Can you please help? – J.D Aug 17 '17 at 13:03
  • return (d[PQ][CASES][CASE]) KeyError: CASES – J.D Aug 17 '17 at 13:20
  • @stovfl: OrderedDict([('@N', 'E90'), ('@RT', 'TG'), ('CASES', OrderedDict([('@ASOF_DATE', '8/11/2017'), ('@CREATE_DATE', '8/13/2017'), ('@RECORDS', '1130'), ('CASE', [OrderedDict([('ID', 'E90'), – J.D Aug 17 '17 at 13:38
  • `'CASES'` is there, so you should be able to access. The only different from your Questions Output, THIS is with enclosing `''`! Next try: `print(d['PQ']['CASES'])`. – stovfl Aug 17 '17 at 13:43
  • Gives me the same key error that I had mentioned above – J.D Aug 17 '17 at 13:49
  • Looks like a **BUG**, give your exact Python Version, I'm on **3.4.2** works OK. Last try `print(list(d['PQ'].values())[2])` – stovfl Aug 17 '17 at 14:09
  • On the same version. I get the following error: parser.ParseFile(xml_input) xml.parsers.expat.ExpatError: mismatched tag: line 1046, column 2 – J.D Aug 17 '17 at 14:12
  • @stovfl I get this as output: OrderedDict([('@ASOF_DATE', '8/11/2017'), ('@CREATE_DATE', '8/13/2017'), ('@RECORDS', '1130'), ('CASE', [OrderedDict([(ID, 'E90') – J.D Aug 17 '17 at 14:37
  • OK, this is the '' Node, now access xy['CASE'] if it fail convert also to `list`. – stovfl Aug 17 '17 at 15:06
  • @stovfl (list(d['PQ'].values())[2])['CASE'] worked!! Thanks! – J.D Aug 17 '17 at 15:07
  • Another Approach worth to try: `d.get('PQ').get('CASES')`. – stovfl Aug 17 '17 at 15:13
  • @stovfl: Thank you so much. Regarding the other question, could you take a look at it? https://stackoverflow.com/questions/45720160/python-local-variable-reference-before-assignment – J.D Aug 17 '17 at 19:18

1 Answers1

0

Question: Unable Return the item of d with key key. Raises a KeyError.
I am trying to access the CASE dictionary.


Data:

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

Tested with Python: 3.4.2

stovfl
  • 14,998
  • 7
  • 24
  • 51
  • Can you please help me with this? https://stackoverflow.com/questions/45745314/convert-python-dictionary-into-html-tables – J.D Aug 17 '17 at 21:39