日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python xml etree_python解析xml文件之xml.etree.cElementTree和xml.etree.ElementTree区别和基本使用...

發布時間:2023/11/30 python 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python xml etree_python解析xml文件之xml.etree.cElementTree和xml.etree.ElementTree区别和基本使用... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、解析速度:ElementTree在 Python 標準庫中有兩種實現。一種是純 Python 實現例如 xml.etree.ElementTree ,另外一種是速度快一點的 xml.etree.cElementTree 。你要記住: 盡量使用 C 語言實現的那種,因為它速度更快,而且消耗的內存更少。

2、調試區別

使用cElementTree的話,在pycharm的debug模式下,是看不到內容的

使用ElementTree,可以看到豐富信息,子節點,子節點的子節點等等,非常方便開發

3、所以對于線上產品應該使用下面這種的方式,但是開發的時候,應該使用 import xml.etree.ElementTree as ET

try:importxml.etree.cElementTree as ETexceptImportError:import xml.etree.ElementTree as ET

tree = ET.parse(config_file)

tree = ET.parse(config_file)或者tree = ET.fromstring(string),可以從文件或者字符串中解析到xml的結構

基本使用:

1、可以從文件或者字符串中解析到xml的結構

從硬盤的文件解析

importxml.etree.ElementTree as ET

tree= ET.parse('country_data.xml')

root= tree.getroot()

直接從字符串解析:

root = ET.fromstring(country_data_as_string)

2、查找元素:

Element.findall() finds only elements with a tag which are direct children of the current element. Element.find() finds the first child with a particular tag, and Element.text accesses the element’s text content. Element.get() accesses the element’s attributes:

3、修改元素:

2、

參考:

1、https://www.biaodianfu.com/python-xml.html

2、https://docs.python.org/2/library/xml.etree.elementtree.html

3、http://effbot.org/zone/element.htm

總結

以上是生活随笔為你收集整理的python xml etree_python解析xml文件之xml.etree.cElementTree和xml.etree.ElementTree区别和基本使用...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。