我应该使用哪个 Python XML 库?
我将处理一个项目的 XML 文件。我早些时候决定使用 lxml,但在阅读了需求之后,我认为 ElemenTree 会更适合我的目的。
必须处理的 XML 文件有:
尺寸较小。通常< 10 KB。
没有命名空间。
简单的 XML 结构。
鉴于 XML 较小,内存不是问题。我唯一关心的是快速解析。
我应该带什么去?大多数情况下,我看到人们推荐 lxml,但考虑到我的解析要求,我真的能从中受益吗?或者 ElementTree 会更好地满足我的目的吗?
I am going to handle XML files for a project. I had earlier decided to use lxml but after reading the requirements, I think ElemenTree would be better for my purpose.
The XML files that have to be processed are:
Small in size. Typically < 10 KB.
No namespaces.
Simple XML structure.
Given the small XML size, memory is not an issue. My only concern is fast parsing.
What should I go with? Mostly I have seen people recommend lxml, but given my parsing requirements, do I really stand to benefit from it or would ElementTree serve my purpose better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如其他人所指出的,lxml 实现了 ElementTree API,因此如果您需要更好的性能或更高级的功能,您可以安全地从 ElementTree 开始并迁移到 lxml。
使用 ElementTree 的一大优势(如果它满足您的需求)是,从 Python 2.5 开始,它是 Python 标准库的一部分,它减少了外部依赖以及处理编译/安装 C 模块的(可能的)头痛。
As others have pointed out, lxml implements the ElementTree API, so you're safe starting out with ElementTree and migrating to lxml if you need better performance or more advanced features.
The big advantage of using ElementTree, if it meets your needs, is that as of Python 2.5 it is part of the Python standard library, which cuts down on external dependencies and the (possible) headache of dealing with compiling/installing C modules.
lxml 基本上是 ElementTree 的超集,因此您可以从 ElementTree 开始,然后如果您遇到性能或功能问题,则可以更改为 lxml。
性能问题只能由你使用自己的数据来研究,
lxml is basically a superset of ElementTree so you could start with ElementTree and then if you have performance or functionality issues then you could change to lxml.
Performance issues can only be studied by you using your own data,
我推荐我自己的食谱
XML Python 数据结构 « Python 食谱 « ActiveState 代码
它不会加快解析速度。但它提供了真正本机对象样式的访问。
I recommend my own recipe
XML to Python data structure « Python recipes « ActiveState Code
It does not speed up parsing. But it provides a really native object style access.