如何在Python 2.7中使用LXML“重置”etree?如何使用lxml重置etree
我有一个包含文件列表的文件。对于此列表中的每个文件,我都将信息存储在LXML的元素树中,然后将LXML写入文件。一旦我写入文件,我想要做的就是将etree恢复到初始状态。
从概念上讲,这是我在哪里:
from lxml import etree
for file in list:
quiz = etree.Element('quiz')
open file and process contents:
"add a bunch of stuff to etree"
etree.SubElement(quiz,'stuff')
"print etree to xml file"
dataOut = etree.tostring(quiz, pretty_print = True)
output_file.write(dataOut)
"reset etree to blank file"
难道你不会在每次迭代中都得到一个全新的'quiz'元素吗?为什么你需要重置树?谢谢。 – alecxe
它看起来像是在etree对象中“堆叠”测验,所以当我写dataOut来将它的前一次迭代和当前迭代连接起来。像文件1 == 1kb,文件2 = 2kb等,内容总是最后加上下一个。 –
明白了。你能显示'etree'来自哪里吗?谢谢。 – alecxe