I’m using Python 3.9 on Windows for the sake of a tutorial.
I’m reading an XML file by using xmltodict. It produces a dictionary with nested keys. I would like to see if the key exists before I assign the value of the element to a variable. I have read about 6 web pages and all the examples only show single-level non-nested keys in a dict.
I have tried these methods. The commented code are other methods I tried and didn’t work for some reason.
if xmldict.get(('something', 'else')):
bad = xmldict.get('something','')
# if xmldict['something']: # Gives KeyError
# bad = xmldict['something']
# try:
# bad = xmldict['something']
# except:
# bad=''
I also tried this, with a key that does exist. This came from the actual Python docs.
if ['Order']['OrderHeader']['Orderid'] in xmldict:
orderid = xmldict['Order']['OrderHeader']['OrderID']
Some methods gave a KeyError. Some didn’t get the data even if the key was in the XML file.
How do I check if the multi-level key exists before I assign the XML contents to a variable?
Alternately, how do I get the contents of an XML element using xmltodict, without seeing a console error if the data does not exist?
At some point I should write up something general about working with nested (JSON-like) data structures. But it’s hard to figure out the scope for something like that.
If that appears in any kind of documentation, I suspect it was meant as pseudocode. It is syntactically valid, but the lefthand side is an expression that would need to be evalauted, and ['Order']['OrderHeader'] is an attempt to index the list ['Order'] with a str value OrderHeader, which isn’t something the list type supports.