将XML转换成字典

时间:2018-07-08 15:51:31

标签: python xml dictionary

我需要将XML文件转换为字典(稍后将其转换为JSON)。

XML脚本示例如下:

<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="Overpass API 0.7.55.3 9da5e7ae">
<note>The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.</note>
<meta osm_base="2018-06-17T15:31:02Z"/>
...
  <node id="2188497873" lat="52.5053306" lon="13.4360114">
    <tag k="alt_name" v="Spreebalkon"/>
    <tag k="name" v="Brommybalkon"/>
    <tag k="tourism" v="viewpoint"/>
    <tag k="wheelchair" v="yes"/>
  </node>
...
</osm>

通过简单的代码,我已经过滤了字典所需的所有值:

代码

import xml.etree.ElementTree as ET

input_file = r"D:\berlin\trial_xml\berlin_viewpoint_locations.xml"

tree = ET.parse(input_file)
root = tree.getroot()

lst1 = tree.findall("./node")
for item1 in lst1:
    print('id:',item1.get('id'))
    print('lat:',item1.get('lat'))
    print('lon:',item1.get('lon'))
    for item1_tags_and_nd in item1.iter('tag'):
        print(item1_tags_and_nd.get('k') + ":", item1_tags_and_nd.get('v'))

结果

id: 2188497873
lat: 52.5053306
lon: 13.4360114
alt_name: Spreebalkon
name: Brommybalkon
tourism: viewpoint
wheelchair: yes

您能帮我吗,请将这些值正确有效地附加到字典中?

我希望它看起来像:

{'id': '2188497873', 'lat': 52.5053306, 'lon': 13.4360114, 'alt_name': 'Spreebalkon', 'name': 'Brommybalkon', 'tourism': 'viewpoint', 'wheelchair': 'yes'}

我尝试过

dictionary = {}
dictionary['id'] = []
dictionary['lat'] = []
dictionary['lon'] = []
lst1 = tree.findall("./node")
for item1 in lst1:
    dictionary['id'].append(item1.get('id'))
    dictionary['lat'].append(item1.get('lat'))
    dictionary['lon'].append(item1.get('lon'))
    for item1_tags_and_nd in item1.iter('tag'):
       dictionary[item1_tags_and_nd.get('k')] = item1_tags_and_nd.get('v')

但是目前还不能正常工作。

1 个答案:

答案 0 :(得分:2)

我建议您构建一个字典列表,而不是像这样的列表字典:

result_list = []
for item in tree.findall("./node"):
    dictionary = {}
    dictionary['id'] = item.get('id')
    dictionary['lat'] = item.get('lat')
    dictionary['lon'] = item.get('lon')
    result_list.append(dictionary)

或者作为comprehensions的一对,例如:

result_list = [{k: item.get(k) for k in ('id', 'lat', 'lon')}
               for item in tree.findall("./node")]

对于嵌套的情况:

result_list = [{k: (item.get(k) if k != 'tags' else
                    {i.get('k'): i.get('v') for i in item.iter('tag')})
                for k in ('id', 'lat', 'lon', 'tags')}
               for item in tree.findall("./node")]

结果:

{
    'id': '2188497873', 
    'lat': '52.5053306', 
    'lon': '13.4360114', 
    'tags': {
         'alt_name': 'Spreebalkon', 
         'name': 'Brommybalkon', 
         'tourism': 'viewpoint', 
         'wheelchair': 'yes'
    }
}