将新值附加到嵌套字典中的内部列表

时间:2020-03-19 16:02:29

标签: python

我正在解析XML文件,我需要填充一个嵌套字典,其中内部字典的键指向列表对象。我需要在每次迭代时向内部列表添加一个新值。

示例XML

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

我需要从解析中得到这样的输出

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

我正在使用xml.etree.ElementTree解析XML。但是,无法在上方填充此类词典。我做的代码

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
...

有什么主意吗?

1 个答案:

答案 0 :(得分:1)

您可以从xml.etree中的每个元素访问子元素。要嵌套字典,您需要在添加子字典时将其添加,并在解析子元素时使用内部字典。

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) 

输出:

print(d)

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