lxml和ElementTree有什么区别? [英] What are the differences between lxml and ElementTree?
问题描述
关于在Python中生成XML数据,我经常看到两个推荐的库: lxml 和
When it comes to generating XML data in Python, there are two libraries I often see recommended: lxml and ElementTree
据我所知,这两个库彼此非常相似.它们似乎都具有相似的模块名称,使用准则和功能.甚至import语句也非常相似.
From what I can tell, the two libraries are very similar to each other. They both seem to have similar module names, usage guidelines, and functionality. Even the import statements are fairly similar.
# Importing lxml and ElementTree
import lxml.etree
import xml.etree.ElementTree
用于Python的lxml
库和ElementTree
库之间有什么区别?
What are the differences between the lxml
and ElementTree
libraries for Python?
推荐答案
ElementTree 内置于Python标准库中,该库包含其他数据模块类型,例如json
和csv
.这意味着该模块随Python的每次安装一起提供.对于大多数常规XML操作(包括构建文档树,简单地搜索和解析元素属性和节点值,甚至是名称空间),ElementTree
是可靠的处理程序.
ElementTree comes built-in with the Python standard library which includes other data modules types such as json
and csv
. This means the module ships with each installation of Python. For most normal XML operations including building document trees and simple searching and parsing of element attributes and node values, even namespaces, ElementTree
is a reliable handler.
Lxml 是需要安装的第三方模块.实际上,由于内置模块中的大多数操作都可用,因此lxml
实际上扩展 ElementTree
.此扩展中的主要功能是lxml
支持XPath 1.0和XSLT 1.0.此外,lxml
可以解析不符合XML的HTML文档,因此可用于网络抓取操作,甚至可以用作 pandas.read_html()
. lxml的其他有用的通用功能包括 pretty_print 输出, objectify
和 sax
支持.当然,作为第三方模块,与标准库相比,具有附加功能的版本也易于访问.
Lxml is a third-party module that requires installation. In many ways lxml
actually extends ElementTree
as most operations in the built-in module are available. Chief among this extension is that lxml
supports both XPath 1.0 and XSLT 1.0. Additionally, lxml
can parse HTML documents that are not XML compliant and hence is used for web-scraping operations and even as the parser in BeautifulSoup and engine in Pandas, pandas.read_html()
. Other useful, common features of lxml include pretty_print output, objectify
, and sax
support. Of course too as a third-party module, versions with additional features are readily accessible compared to the standard library.
这篇关于lxml和ElementTree有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!