【问题标题】:Extract and store(as csv) attribute tags from an xml file从 xml 文件中提取和存储(作为 csv)属性标签
【发布时间】:2021-03-04 07:30:21
【问题描述】:

我正在尝试从 xml 文件创建一个包含给定时间点车辆数量的 .csv 文件。

    <fcd-export xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/fcd_file.xsd">
    <timestep time="0.00">
        <vehicle id="veh0" x="668.78" y="2768.63" angle="265.68" type="veh_passenger" speed="0.00" pos="5.10" lane="253751722#0_1" slope="0.00"/>
    </timestep>
    <timestep time="1.00">
        <vehicle id="veh0" x="666.91" y="2768.48" angle="265.68" type="veh_passenger" speed="1.88" pos="6.98" lane="253751722#0_1" slope="0.00"/>
    </timestep>
    <timestep time="2.00">
        <vehicle id="veh0" x="663.10" y="2768.20" angle="265.68" type="veh_passenger" speed="3.82" pos="10.80" lane="253751722#0_1" slope="0.00"/>
    </timestep>
    <timestep time="3.00">
        <vehicle id="veh0" x="656.86" y="2767.72" angle="265.68" type="veh_passenger" speed="6.25" pos="17.05" lane="253751722#0_1" slope="0.00"/>
    </timestep>
    <timestep time="4.00">
        <vehicle id="veh0" x="648.85" y="2767.12" angle="265.68" type="veh_passenger" speed="8.04" pos="25.09" lane="253751722#0_1" slope="0.00"/>
    </timestep>
    <timestep time="5.00">
        <vehicle id="veh0" x="639.02" y="2766.38" angle="265.68" type="veh_passenger" speed="9.85" pos="34.93" lane="253751722#0_1" slope="0.00"/>
    </timestep>
<timestep time="14.00">
        <vehicle id="veh0" x="619.10" y="2768.08" angle="85.68" type="veh_passenger" speed="8.01" pos="20.23" lane="-253751722#0_1" slope="0.00"/>
        <vehicle id="veh1" x="988.73" y="2991.84" angle="226.12" type="veh_passenger" speed="3.87" pos="11.45" lane="-30968089#3_0" slope="0.00"/>
    </timestep>
    <timestep time="15.00">
        <vehicle id="veh0" x="628.97" y="2768.83" angle="85.68" type="veh_passenger" speed="9.90" pos="30.13" lane="-253751722#0_1" slope="0.00"/>
        <vehicle id="veh1" x="984.27" y="2987.44" angle="225.26" type="veh_passenger" speed="6.25" pos="17.70" lane="-30968089#3_0" slope="0.00"/>
    </timestep>
    <timestep time="16.00">
        <vehicle id="veh0" x="640.26" y="2769.68" angle="85.68" type="veh_passenger" speed="11.32" pos="41.45" lane="-253751722#0_1" slope="0.00"/>
        <vehicle id="veh1" x="978.85" y="2981.98" angle="224.80" type="veh_passenger" speed="7.69" pos="25.39" lane="-30968089#3_0" slope="0.00"/>
    </timestep>
    <timestep time="14.00">
        <vehicle id="veh0" x="619.10" y="2768.08" angle="85.68" type="veh_passenger" speed="8.01" pos="20.23" lane="-253751722#0_1" slope="0.00"/>
        <vehicle id="veh1" x="988.73" y="2991.84" angle="226.12" type="veh_passenger" speed="3.87" pos="11.45" lane="-30968089#3_0" slope="0.00"/>
    </timestep>
    <timestep time="15.00">
        <vehicle id="veh0" x="628.97" y="2768.83" angle="85.68" type="veh_passenger" speed="9.90" pos="30.13" lane="-253751722#0_1" slope="0.00"/>
        <vehicle id="veh1" x="984.27" y="2987.44" angle="225.26" type="veh_passenger" speed="6.25" pos="17.70" lane="-30968089#3_0" slope="0.00"/>
    </timestep>
    <timestep time="16.00">
        <vehicle id="veh0" x="640.26" y="2769.68" angle="85.68" type="veh_passenger" speed="11.32" pos="41.45" lane="-253751722#0_1" slope="0.00"/>
        <vehicle id="veh1" x="978.85" y="2981.98" angle="224.80" type="veh_passenger" speed="7.69" pos="25.39" lane="-30968089#3_0" slope="0.00"/>
    </timestep>

这是我到目前为止所做的:

      import xml.etree.ElementTree as ET
      xml_tree = ET.parse("trace.xml")
      root = xml_tree.getroot()
      while i<150:
          print(root[i][1].get('id'))
          i=i+1

我尝试使用以下命令对标签进行索引:root[x][0].get('id') 这里 x 代表时间。 但是当存在多个车辆标签时,它只会返回其中一个属性。我对 python 比较陌生,任何帮助都将不胜感激。谢谢

【问题讨论】:

  • 你能发布你到目前为止所做的代码吗?
  • 将代码放在问题中而不是 cmets 中。您可以编辑问题。
  • 对不起,我是这个平台的新手,我已经添加了代码
  • 您能否编辑您的问题并在您的 xml 中添加一个条目“当存在多个车辆标签时”?另外,您的预期输出究竟是什么?
  • 如果你查看&lt;timestep time="15"&gt;,你可以找到`veh1`和veho。到目前为止,我只是想在给定的时间点为每辆车获取id属性我的输出。

标签: python xml parsing


【解决方案1】:

对于这种类型的工作,最好使用xpath和lxml:

from lxml import etree
vehicles = """[your xml]"""
doc = etree.XML(vehicles.encode())
for ts in doc.xpath('//timestep'):
    print('time: ',ts.attrib['time'])
    for vehicle in ts.xpath('./vehicle/@id'):
        print('\tvehicle id:',vehicle)

输出:

.....
time:  4.00
    vehicle id: veh0
time:  5.00
    vehicle id: veh0
time:  14.00
    vehicle id: veh0
    vehicle id: veh1
time:  15.00
    vehicle id: veh0
    vehicle id: veh1
.....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 2011-12-14
    相关资源
    最近更新 更多