1 回答
TA贡献1796条经验 获得超4个赞
当前代码对您不起作用的原因是数据结构不同。我建议为每个node和way类型使用独立的解析器。您已经在解析node类型,因此要解析,way可以构造一个非常简单的循环,如下所示:
way_list = []
for item in tree.findall("./way"):
# get the center node
center = item.find('center')
# get the refs for the nd nodes
nds = [nd.get('ref') for nd in item.iter('nd')]
# construct a dict and append to result list
way_list.append(dict(
id=item.get('id'),
lat=center.get('lat'),
lon=center.get('lon'),
nds=nds,
extra={i.get('k'): i.get('v') for i in item.iter('tag')},
))
print(way_list)
结果:
[{
'id': '281307598',
'lat': '52.4934004',
'lon': '13.4843019',
'nds': ['2852755795', '3772363803', '3772363802', '2852755796',
'2852755797', '2852755798', '2852755795'],
'extra': {
'man_made': 'tower',
'tourism': 'viewpoint',
'tower:type': 'observation',
'wheelchair': 'yes'
}
}]
添加回答
举报