如何从 Python 快速流式传输 XML 输出 [英] How to stream XML output quickly from Python

查看:43
本文介绍了如何从 Python 快速流式传输 XML 输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

迭代编写 XML 文件的快速方法是什么(即无需将整个文档保存在内存中)?xml.sax.saxutils.XMLGenerator 可以工作,但速度很慢,在 I7 机器上大约为 1MB/s.这是一个测试用例.

What is a quick way of writing an XML file iteratively (i.e. without having the whole document in memory)? xml.sax.saxutils.XMLGenerator works but is slow, around 1MB/s on an I7 machine. Here is a test case.

推荐答案

我意识到这个问题已经被问过一段时间了,但与此同时,一个 lxml API 已经被引入,看起来有希望解决这个问题:http://lxml.de/api.html ;具体参考以下章节:增量XML生成".

I realize that this question has been asked awhile ago, but, in the mean time, an lxml API has been introduced that looks promising in terms of addressing the problem: http://lxml.de/api.html ; specifically, refer to the following section: "Incremental XML generation".

我像在您的基准测试中一样通过流式传输 10M 文件快速对其进行了测试,并且在我的旧笔记本电脑上花费了几分之一秒,这绝不是非常科学,但与您的 generate_large_xml() 函数.

I quickly tested it by streaming a 10M file just as in your benchmark, and it took a fraction of a second on my old laptop, which is by no means very scientific, but is quite in the same ballpark as your generate_large_xml() function.

这篇关于如何从 Python 快速流式传输 XML 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆