0

我似乎被困住了。我正在尝试获取从我的 python 代码生成的 xml 的多次迭代。它只将最新的迭代写入文件。

这是我的代码:

import xml.etree.cElementTree as ET
from random import randint

i=5
while i>-4:
  event = ET.Element("event")
  event.set("class", "rer")
  event.set("id", "EV_%d" % (i))
  event.set("date", "${ev_d}")

  loc = ET.SubElement(event, "loc")
  flir = ET.SubElement(loc, "flir")

  de = ET.SubElement(flir, "de")
  de.set("dist", "km")
  de.set("val", "%d" % (randint(0,2)))

  i -=1   

  tree = ET.ElementTree(event)
  tree.write("test.xml")

我得到什么:

<event class="rer" date="${ev_d}" id="EV_-3">
  <loc>
    <flir>
      <de dist="km" val="0" />
    </flir>
  </loc>
</event>

我想要的输出文件:

<event class="rer" date="${ev_d}" id="EV_5">
  <loc>
    <flir>
      <de dist="km" val="0" />
    </flir>
  </loc>
</event>
<event class="rer" date="${ev_d}" id="EV_4">
  <loc>
    <flir>
      <de dist="km" val="0" />
    </flir>
  </loc>
</event>
<event class="rer" date="${ev_d}" id="EV_3">
  <loc>
    <flir>
      <de dist="km" val="0" />
    </flir>
  </loc>
</event>

在此先感谢您的帮助。

问候, 优点_2

4

2 回答 2

1

正如@jwodder 指出的那样,您建议的输出不是有效的 XML。你的选择是解决这个问题或绕过它。既然您提到这只是一个组件,那么如果您有一个每个<event>都将嵌套在其下的父元素,那么这是一种方法。

import xml.etree.cElementTree as ET
from random import randint

parent = ET.Element("parent")
tree = ET.ElementTree(parent)

i=5
while i>-4:
    event = ET.Element("event")
    event.set("class", "rer")
    event.set("id", "EV_%d" % (i))
    event.set("date", "${ev_d}")

    loc = ET.SubElement(event, "loc")
    flir = ET.SubElement(loc, "flir")

    de = ET.SubElement(flir, "de")
    de.set("dist", "km")
    de.set("val", "%d" % (randint(0,2)))

    i -=1   

    parent.append(event)

tree.write("test.xml")

在这里,我将父元素和append-ed 事件添加到父元素,然后将父树写到最后的文件中。

输出:

<parent>
  <event class="rer" date="${ev_d}" id="EV_5">
    <loc>
      <flir>
        <de dist="km" val="1" />
      </flir>
    </loc>
  </event>
  <event class="rer" date="${ev_d}" id="EV_4">
    <loc>
      <flir>
        <de dist="km" val="2" />
      </flir>
    </loc>
  </event>
  <event class="rer" date="${ev_d}" id="EV_3">
    <loc>
      <flir>
        <de dist="km" val="2" />
      </flir>
    </loc>
  </event>
  <event class="rer" date="${ev_d}" id="EV_2">
    <loc>
      <flir>
        <de dist="km" val="1" />
      </flir>
    </loc>
  </event>
  <event class="rer" date="${ev_d}" id="EV_1">
    <loc>
      <flir>
        <de dist="km" val="2" />
      </flir>
    </loc>
  </event>
  <event class="rer" date="${ev_d}" id="EV_0">
    <loc>
      <flir>
        <de dist="km" val="0" />
      </flir>
    </loc>
  </event>
  <event class="rer" date="${ev_d}" id="EV_-1">
    <loc>
      <flir>
        <de dist="km" val="2" />
      </flir>
    </loc>
  </event>
  <event class="rer" date="${ev_d}" id="EV_-2">
    <loc>
      <flir>
        <de dist="km" val="0" />
      </flir>
    </loc>
  </event>
  <event class="rer" date="${ev_d}" id="EV_-3">
    <loc>
      <flir>
        <de dist="km" val="2" />
      </flir>
    </loc>
  </event>
</parent>
于 2016-03-15T17:16:33.817 回答
1

您在 while 循环中不断地覆盖事件 - 所以自然而然地您只需要打印事件。您需要执行以下操作:

import xml.etree.cElementTree as ET
from random import randint

i = 5
events = []
while i > -4:
    event = ET.Element("event")
    event.set("class", "rer")
    event.set("id", "EV_%d" % (i))
    event.set("date", "${ev_d}")
    loc = ET.SubElement(event, "loc")
    flir = ET.SubElement(loc, "flir")
    de = ET.SubElement(flir, "de")
    de.set("dist", "km")
    de.set("val", "%d" % (randint(0, 2)))
    events.append(event)
    i -= 1

with open('output.xml', 'w') as out:
    for event in events:
        tree = ET.ElementTree(event)
        tree.write(out)
        out.write('\n')
于 2016-03-15T16:57:19.210 回答