I'm using xmltodict to parse an XML config. The XML has structures where an element can occur in 1 to n instances, where both are valid:
<items>
<item-ref>abc</item-ref>
</items>
and
<items>
<item-ref>abc</item-ref>
<item-ref>dca</item-ref>
<item-ref>abb</item-ref>
</items>
I'm parsing this with xmltodict as follows:
document['items']['item-ref']
and it gives back a single unicode or a list (depending the items found), so I always need to add an extra check to ensure if I need to handle a list or a string:
if isinstance(document['items']['item-ref'], list):
my_var = document['items']['item-ref']
else:
my_var = [document['items']['item-ref']] #create list manually
Is there a better/simpler/more elegant way to handle these?
I am not sure of a super elegant way to do this. Python doesn't have any built-in methods or functions that will help you do this in a single line of code. Nevertheless, in the interest of consolidating code, you will want to do something. As matsjoyce mentioned in a comment, you may simply want to create a function that will take care of this logic for you.
Now you can simply call that function with your val and expect a
list
to be returned, whether it is alist
, anint
, or aNoneType
object:(My first answer created a subclass of
list
, but that takes 6 lines instead of 4 and changes thetype
--not the most ideal side-effect.)