第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將XML轉(zhuǎn)換為Dict / JSON

將XML轉(zhuǎn)換為Dict / JSON

慕斯王 2021-04-09 14:15:52
我需要將XML文件轉(zhuǎn)換為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="330268305" lat="52.5475000" lon="13.3850775">    <tag k="direction" v="240-60"/>    <tag k="tourism" v="viewpoint"/>    <tag k="wheelchair" v="no"/>  </node>  <node id="330269757" lat="52.5473115" lon="13.3843131">    <tag k="direction" v="240-60"/>    <tag k="tourism" v="viewpoint"/>    <tag k="wheelchair" v="limited"/>  </node>  <way id="281307598">    <center lat="52.4934004" lon="13.4843019"/>    <nd ref="2852755795"/>    <nd ref="3772363803"/>    <nd ref="3772363802"/>    <nd ref="2852755796"/>    <nd ref="2852755797"/>    <nd ref="2852755798"/>    <nd ref="2852755795"/>    <tag k="man_made" v="tower"/>    <tag k="tourism" v="viewpoint"/>    <tag k="tower:type" v="observation"/>    <tag k="wheelchair" v="yes"/>  </way></osm>到目前為止執(zhí)行的代碼。import xml.etree.ElementTree as ETimport jsoninput_file = r"D:\berlin\trial_xml\berlin_viewpoint_locations.xml"tree = ET.parse(input_file)root = tree.getroot()result_list = [{k: (item.get(k) if k != 'extra' else                    {i.get('k'): i.get('v') for i in item.iter('tag')})                for k in ('id', 'lat', 'lon', 'extra')}               for item in tree.findall("./node") + tree.findall('./way')]print(result_list)在一些Stackoverflow專家的協(xié)助下,我已經(jīng)取得了半完成的結(jié)果。但是,我仍然需要了解如何:追加坐標(biāo),該坐標(biāo)隱藏在此處提到<center lat="52.4934004" lon="13.4843019"/>的相同result_list fornodes.It works for“ id”中。附加所有引用<nd ref="2852755795"/> <nd ref="3772363803"/>,方法與之相同extra,例如,嵌套列表。
查看完整描述

1 回答

?
SMILET

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'    

    }

}]


查看完整回答
反對 回復(fù) 2021-04-27
  • 1 回答
  • 0 關(guān)注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號