edmpg
edmpg

Reputation: 3

Appending a new value to inner list in a nested dictionary

I am parsing an XML file and I need to populate a nested dictionary where keys of inner dictionary points a list object. I need to append a new value to the inner list at every iteration.

Sample XML

<entry name = 'Europe'>
  <entry name = 'Spain'>
    <from>'Madrid'</from>
    <from>'Barcelona'</from>
  <entry name = 'Nederland'>
    <from>'Amsterdam'</from>
    <from>'Roterdam'</from>

I need to have such output from parsing

d = {'Europe':{'Spain':['Madrid','Barcelona']}, {'Nederland':['Amsterdam','Roterdam']}

I am using xml.etree.ElementTree to parse XML. However, couldn't populate such a dictionary above. The code I did

import xml.etree.ElementTree as ET
tree = ET.parse(europe.xml)
root = tree.getroot()
for i in root.findall('.//entry'):
   for j in root.findall(''.//entry/):
      l = []
      l_new = [:]
      for k in root.findall('.//entry/entry/):
         l_new.append(k.text)
         d1[j] = l_new
...

Any idea?

Upvotes: 0

Views: 48

Answers (1)

Alain T.
Alain T.

Reputation: 42143

You can access the sub elements from each element in the xml.etree. To nest dictionaries, you'll need to add them as you go and work with the inner dictionaries when you are parsing the sub-elements.

europe="""<xml>
<entry name = 'Europe'>
  <entry name = 'Spain'>
    <from>'Madrid'</from>
    <from>'Barcelona'</from>
  </entry>
  <entry name = 'Nederland'>
    <from>'Amsterdam'</from>
    <from>'Roterdam'</from>
  </entry>
</entry>
</xml>"""

import xml.etree.ElementTree as ET
root = ET.fromstring(europe)
d = dict()
for rg in root.findall("entry"):
    # add the region level to the dictionary (e.g. "europe") 
    d[rg.attrib["name"]] = region = {} 
    for cn in rg.findall("entry"):
        # add the country to the current region level (e.g. "spain")
        region[cn.attrib["name"]] = country = []
        for city in cn.findall("from"):
            # add the city to the current region
            country.append(city.text) 

output:

print(d)

{'Europe':
    {'Spain': ["'Madrid'", "'Barcelona'"],
     'Nederland': ["'Amsterdam'", "'Roterdam'"]}}

Upvotes: 1

Related Questions