python读写xml用什么包最好_python处理xml的常用包(lib.xml、ElementTree、lxml)
python处理xml的三种常见机制dom(随机访问机制)sax(Simple APIs for XML,事件驱动机制)etreepython处理xml的三种包标准库中的xmlFredrik Lundh 的 ElementTreeStefan Behnel 的 lxml对以上三种包的介绍和对比With the continued growth of both Python and XML, the
python处理xml的三种常见机制
dom(随机访问机制)
sax(Simple APIs for XML,事件驱动机制)
etree
python处理xml的三种包
标准库中的xml
Fredrik Lundh 的 ElementTree
Stefan Behnel 的 lxml
对以上三种包的介绍和对比
With the continued growth of both Python and XML, there is a plethora(过剩/过多) of packages out there that help you read, generate, and modify XML files from Python scripts. Compared to most of them, the lxml package has two big advantages:
Performance. Reading and writing even fairly large XML files takes an almost imperceptible(小得无法察觉的) amount of time.
Ease of programming. The lxml package is based on ElementTree, which Fredrik Lundh invented to simplify and streamline XML processing.
lxml is similar in many ways to two other, earlier packages:
Fredrik Lundh continues to maintain his original version of ElementTree.
xml.etree.ElementTree is now an official part of the Python library. There is a C-language version called cElementTree which may be even faster than lxml for some applications.
However, the author prefers lxml for providing a number of additional features that make life easier. In particular, support for XPath makes it considerably easier to manage more complex XML structures.
标准库中的xml包
The XML handling submodules are:
xml.etree.ElementTree: the ElementTree API, a simple and lightweight XML processor
xml.dom: the DOM API definition
xml.dom.minidom: a minimal DOM implementation
xml.dom.pulldom: support for building partial DOM trees
xml.sax: SAX2 base classes and convenience functions
xml.parsers.expat: the Expat parser binding
ElementTree包
The Element type is a flexible container object, designed to store hierarchical data structures in memory. Element structures can be converted to and from XML.
其作者对lxml的推介:http://effbot.org/zone/element-index.htm
There’s also an independent implementation, lxml.etree, based on the well-known libxml2/libxslt libraries. This adds full support for XSLT, XPath, and more.
lxml介绍
lxml - XML and HTML with Python
lxml is the most feature-rich and easy-to-use library for processing XML and HTML in the Python language.
The lxml XML toolkit is a Pythonic binding for the C libraries libxml2 and libxslt. It is unique in that it combines the speed and XML feature completeness of these libraries with the simplicity of a native Python API, mostly compatible but superior to the well-known ElementTree API. The latest release works with all CPython versions from 2.6 to 3.6.
总结
一般情况下使用lxml可获得高效率和易用性。
扩展阅读
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)