将XML转换为Dict / JSON

我需要将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="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>

到目前为止执行的代码。


import xml.etree.ElementTree as ET

import json


input_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专家的协助下,我已经取得了半完成的结果。但是,我仍然需要了解如何:


追加坐标,该坐标隐藏在此处提到<center lat="52.4934004" lon="13.4843019"/>的相同result_list fornodes.

It works for“ id”中。

附加所有引用<nd ref="2852755795"/> <nd ref="3772363803"/>,方法与之相同extra,例如,嵌套列表。


慕斯王
浏览 131回答 1
1回答

SMILET

当前代码对您不起作用的原因是数据结构不同。我建议为每个node和way类型使用独立的解析器。您已经在解析node类型,因此要解析,way可以构造一个非常简单的循环,如下所示:way_list = []for item in tree.findall("./way"):&nbsp; &nbsp; # get the center node&nbsp; &nbsp; center = item.find('center')&nbsp; &nbsp; # get the refs for the nd nodes&nbsp; &nbsp; nds = [nd.get('ref') for nd in item.iter('nd')]&nbsp; &nbsp; # construct a dict and append to result list&nbsp; &nbsp; way_list.append(dict(&nbsp; &nbsp; &nbsp; &nbsp; id=item.get('id'),&nbsp; &nbsp; &nbsp; &nbsp; lat=center.get('lat'),&nbsp; &nbsp; &nbsp; &nbsp; lon=center.get('lon'),&nbsp; &nbsp; &nbsp; &nbsp; nds=nds,&nbsp; &nbsp; &nbsp; &nbsp; extra={i.get('k'): i.get('v') for i in item.iter('tag')},&nbsp; &nbsp; ))print(way_list)结果:[{&nbsp; &nbsp; 'id': '281307598',&nbsp;&nbsp; &nbsp; 'lat': '52.4934004',&nbsp;&nbsp; &nbsp; 'lon': '13.4843019',&nbsp;&nbsp; &nbsp; 'nds': ['2852755795', '3772363803', '3772363802', '2852755796',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '2852755797', '2852755798', '2852755795'],&nbsp; &nbsp; 'extra': {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'man_made': 'tower',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'tourism': 'viewpoint',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'tower:type': 'observation',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'wheelchair': 'yes'&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }}]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python