python cElementtree 和 ElementTree 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2351694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the Difference between cElementtree and ElementTree?
提问by Hai Vu
I know a little of dom, and would like to learn about ElementTree. Python 2.6 has a somewhat older implementation of ElementTree, but still usable. However, it looks like it comes with two different classes: xml.etree.ElementTree and xml.etree.cElementTree. Would someone please be so kind to enlighten me with their differences? Thank you.
我知道一点 dom,并想了解 ElementTree。Python 2.6 有一个较旧的 ElementTree 实现,但仍然可用。但是,看起来它带有两个不同的类:xml.etree.ElementTree 和 xml.etree.cElementTree。有人会这么好心地告诉我他们的差异吗?谢谢你。
回答by Desintegr
It is the same library (same API, same features) but ElementTree is implemented in Python and cElementTree is implemented in C.
它是相同的库(相同的 API,相同的功能),但是 ElementTree 是用 Python 实现的,而 cElementTree 是用 C 实现的。
If you can, use the C implementation because it is optimized for fast parsing and low memory use, and is 15-20 times faster than the Python implementation.
如果可以,请使用 C 实现,因为它针对快速解析和低内存使用进行了优化,并且比 Python 实现快 15-20 倍。
Use the Python version if you are in a limited environment (C library loading not allowed).
如果您处于有限的环境中(不允许加载 C 库),请使用 Python 版本。
回答by logan
But now they are the same thing as of Python 3.3, in github source code cElementTree
但现在它们与 Python 3.3相同,在 github 源代码cElementTree
# cElementTree.py
from xml.etree.ElementTree import *
# cElementTree.py
从 xml.etree.ElementTree 导入 *
it is just for backward compatibility
它只是为了向后兼容
回答by Benjamin Wohlwend
From http://effbot.org/zone/celementtree.htm:
来自http://effbot.org/zone/celementtree.htm:
The cElementTree module is a C implementation of the ElementTree API, optimized for fast parsing and low memory use. On typical documents, cElementTree is 15-20 times faster than the Python version of ElementTree, and uses 2-5 times less memory
cElementTree 模块是 ElementTree API 的 C 实现,针对快速解析和低内存使用进行了优化。在典型文档上,cElementTree 比 Python 版本的 ElementTree 快 15-20 倍,并且使用的内存少 2-5 倍
回答by jcdyer
ElementTree is implemented in python while cElementTree is implemented in C. Thus cElementTree will be faster, but also not available where you don't have access to C, such as in Jython or IronPython or on Google App Engine.
ElementTree 在 python 中实现,而 cElementTree 在 C 中实现。因此 cElementTree 会更快,但在您无法访问 C 的情况下也不可用,例如在 Jython 或 IronPython 或 Google App Engine 上。
Functionally, they should be equivalent.
在功能上,它们应该是等效的。
回答by Kirill Malakhov
From https://docs.python.org/3/library/xml.etree.elementtree.html:
从https://docs.python.org/3/library/xml.etree.elementtree.html:
Changed in version 3.3: This module will use a fast implementation whenever available. The xml.etree.cElementTree module is deprecated.
在 3.3 版更改:此模块将在可用时使用快速实现。xml.etree.cElementTree 模块已弃用。
So for Python 3.3 and higher just use:
因此,对于 Python 3.3 及更高版本,只需使用:
import xml.etree.ElementTree as ET