非XML方式的Python XML编写器

时间:2014-11-27 00:44:34

标签: python xml

我使用ElementTree作为我的python xml util。但是最近我写了一个问题。在我的代码中,它收集大约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!

0 个答案:

没有答案