PythonXML编写器的非留置方式

PythonXML编写器的非留置方式,python,xml,Python,Xml,我使用ElementTree作为python xml工具。但最近我在写它的时候遇到了一个问题。在我的代码中,它收集了大约10MB的数据,并在代码末尾将其写入。所以大约需要10到12秒。但是在进程中,编写进程大约需要8到10秒。 我希望更改代码以节省时间。作为一个普通的文件编写代码,是否有任何库以非线性方式工作 补充 我通常是这样写的 from xml.etree.cElementTree import Element, ElementTree rootEm = Element("root") c

我使用ElementTree作为python xml工具。但最近我在写它的时候遇到了一个问题。在我的代码中,它收集了大约10MB的数据,并在代码末尾将其写入。所以大约需要10到12秒。但是在进程中,编写进程大约需要8到10秒。
我希望更改代码以节省时间。作为一个普通的文件编写代码,是否有任何库以非线性方式工作

补充 我通常是这样写的

from xml.etree.cElementTree import Element, ElementTree
rootEm = Element("root")
childEm1 = Element("child1")
rootEm.append(childEm1)
childEm2 = Element("child2")
rootEm.append(childEm2)
ElementTree(rootEm).write(filename)        ## I always save it at the last time of process
但是我想要这样的工作

import SOMEXMLWRITER
_f = open(filename, "w")                   ## open file first
writer = SOMEXMLWRITER(_f)
rootEm = Element("root")                   ## add Element at that time
writer.addXMLDocument(rootEm)
childEm1 = Element("child1")
rootEm.append(childEm1)
childEm2 = Element("child2")
rootEm.append(childEm2)
_f.close()                                 ## just close file... DONE!

另请参见,可能相关:和。@alecxe我添加了一个示例来解释我的目的。