我需要将XML文件转换成字典(稍后将转换为JSON)。在
XML脚本示例如下:<?xml version="1.0" encoding="UTF-8"?>
The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.
...
...
通过简单的代码,我已经过滤了字典中需要的所有值:
编码
^{pr2}$
结果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')
但到目前为止还不管用。在