多次将xml写入文件python

时间:2016-03-15 16:11:48

标签: python xml xml-generation

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

2 个答案:

答案 0 :(得分: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')

答案 1 :(得分: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>