1 回答

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個贊
當(dāng)前代碼對您不起作用的原因是數(shù)據(jù)結(jié)構(gòu)不同。我建議為每個node和way類型使用獨(dú)立的解析器。您已經(jīng)在解析node類型,因此要解析,way可以構(gòu)造一個非常簡單的循環(huán),如下所示:
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)
結(jié)果:
[{
'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'
}
}]
添加回答
舉報(bào)