多次将 xml 写入文件 python

writing xml to file python multiple times

我好像卡住了。我正在尝试获取从我的 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>

在此先感谢您的帮助。

此致, merit_2

您在 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')

正如@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>