Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

python - What are the differences between lxml and ElementTree?

When it comes to generating XML data in Python, there are two libraries I often see recommended: lxml and ElementTree

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

What are the differences between the lxml and ElementTree libraries for Python?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...