使用pythonlxm连续写入输出文件

2024-06-26 02:11:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在把数据库中的大块数据写到XML文件中。我使用Python及其lxml库来创建文件。 我注意到它在内存中生成整个XML文件,然后在最后输出到文件中,有没有办法为每X个数据库对象转储XML文件?在

import lxml.etree as etree
import os

root = etree.Element('root')
db_obj1 = etree.SubElement(root, 'item')
db_obj2 = etree.SubElement(root, 'item')
db_obj3 = etree.SubElement(root, 'item')
et = etree.ElementTree(root)
et.write(sys.stdout)

我尝试过ElemenTree().write(),但是在http://lxml.de/api/lxml.etree._ElementTree-class.html的文档中找不到任何关于如何实现这一点的设置或最佳实践。在

希望减少总内存占用。在


Tags: 文件内存import数据库dbrootxmlitem
1条回答
网友
1楼 · 发布于 2024-06-26 02:11:30

来自the lxml docs

It is a common pattern to have one or more nested element() blocks, and then build in-memory XML subtrees in a loop (using the ElementTree API, the builder API, XSLT, or whatever) to write them out into the XML file one after the other. That way, they can be removed from memory right after their construction, which CAN LARGELY REDUCE THE MEMORY FOOTPRINT of an application, while keeping the overall XML generation easy, safe and correct. [Emphasis mine].

filename = "/tmp/somefile.xml"
with ET.xmlfile(filename, encoding='utf-8') as xf:
    xf.write_declaration(standalone=True)
    xf.write_doctype('<!DOCTYPE root SYSTEM "some.dtd">')
    with xf.element('root'):
        for value in '123':
            # construct a really complex XML tree
            el = ET.Element('item', attr=value)
            xf.write(el)
            # no longer needed, discard it right away!
            el = None

^{pr2}$

到文件中。在

相关问题 更多 >