I am parsing some XML data from the Open Street Map in JSON (it will later be loaded into the database). It is big, so I use iterparse. I have tags that look like this:
<tag 'k'=stringA:stringB:stringC 'v'=value>
which I want to analyze in
{stringA: {stringB: {stringC: value} } }
I did this with ugly hard code. However, I would like to create a function that uses recursion to solve the same problem if the value of the attribute "k" contains an arbitrary number ":".
I created this
def nestify(l):
"""Takes a list l and returns nested dictionaries where each element from
the list is both a key to the inner dictionary and a value to the outer,
except for the last element of the list, one which is only a value.
For best understanding, feed w = [ 'a', 'b', 'c', 'd', 'e', 'f', 'v'] to the
function and look at the output."""
n = len(l)
if n==2:
key = l[0]
value = l[1]
else:
key = l[0]
value = nestify(l[1:])
return {key:value}
which is working. (You must first make the keys and value in the list.) Also, that is not the case, because he always makes a new dictionary. I need him to respect the previous data and integrate it. For example, if my parser is found
<tag 'k'=a:b:c 'v'=1 />
and then in the same element
<tag 'k'=a:d:e 'v'=2 />
{a: {'b': {'c' : 1}, 'd' : {'e' : 2}}}
{a: {'b' : {'c' : 1}}}
{a: {'d' : {'e' : 2}}}
:
def smart_nestify(l, record):
n = len(l)
if n==2:
key = l[0]
value = l[1]
else:
key = l[0]
value = smart_nestify(l[1:], key)
if key not in record:
return {key:value}
else:
record[key] = value
return record
. ? ?