I have converted some XML files with xmltodict
to native Python types (so it "feel[s] like [I am] working with JSON"). The converted objects have a lot of "P"
keys with values that might be one of:
- a list of strings
- a list of
None
and a string. - a list of dicts
- a list of lists of dicts
If the list contain only strings or if the list contain only strings and None
, then it should be converted to a string using join. If the list contain dict
s or list
s then it should be skipped without processing.
How can the code tell these cases apart, so as to determine which action should be performed?
Example data for the first two cases, which should be joined:
["Bla Bla"]
[null,"Bla bla"]
Example data for the last two cases, which should be skipped:
[{"CPV_CODE":{"CODE":79540000}}]
[
[{"CPV_CODE":{"CODE":79530000}}, {"CPV_CODE":{"CODE":79540000}}],
[{"CPV_CODE":{"CODE":79550000}}]
]
This is done in a function that processes the data:
def recursive_iter(obj):
if isinstance(obj, dict):
for item in obj.values():
if "P" in obj and isinstance(obj["P"], list) and not isinstance(obj["P"], dict):
#need to add a check for not dict and list in list
obj["P"] = " ".join([str(e) for e in obj["P"]])
else:
yield from recursive_iter(item)
elif any(isinstance(obj, t) for t in (list, tuple)):
for item in obj:
yield from recursive_iter(item)
else:
yield obj
Since you want to find the list with strings
is it what you want?