python標準庫ElementTree處理xml
1. 示例用法
參照官方文檔,創(chuàng)建country_data.xml測試文檔,內(nèi)容如下:
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>使用如下代碼,將數(shù)據(jù)讀出,打印
from xml.etree.ElementTree
data = ElementTree.ElementTree(file='country_data.xml')
country_list = data.findall('country') #找到所有名為‘country'的tag,返回一個Element對象列表。
for country in country_list:
name = country.attrib.get('name', '')
print name, ' ',
for item in country:
if item.tag == 'neighbor':
name = item.attrib.get('name', '')
direction = item.attrib.get('direction', '')
print '{0} ({1})'.format(name, direction), ' ',
else:
print item.text, ' ',
print ''其中
data = ElementTree.ElementTree(file='country_data.xml')
獲得一個ElementTree對象,也可以使用
tree = ElementTree.parse('country_data.xml')
Element對象具有如下屬性和操作
| elem.tag | 這個Element對象的名字(tag) |
| elem.text | 文檔內(nèi)容 |
| elem.attrib | 屬性值字典 |
| elem.tail | 與屬性一起存儲的其他數(shù)據(jù) |
elem[n] 返回elem的第n個子元素
elem[n] = new_elem 將elem的第n個子元素更改為不同的元素new_elem
del elem[n] 刪除子元素
len(elem) 子元素的數(shù)量
elem.find(path)
elem.getchildren() 按文檔順序返回所有子元素
elem.items()將所有元素的屬性值以(name, value)對列表形式返回
遇到非法格式的xml
ExpatError: no element found
bad.xml為空文檔時,內(nèi)容如下:
<?xml version="1.0"?>
執(zhí)行如下python代碼,遇到xml.parser.expat.ExpatError異常:
import xml.etree.ElementTree as ET
ET.parse('bad.xml')xml.parsers.expat.ExpatError: no element found: line 3, column 0
ExpatError: mismatched tag
bad.xml中找不到對應結束標記符時,內(nèi)容如下:
<?xml version="1.0"?> <note> </Note>
因為區(qū)分大小寫,所以</Note> 不能作為<note>的結束標記。
xml.parsers.expat.ExpatError: mismatched tag: line 3, column 2
ExpatError: not well-formed(invalid token)
bad.xml中屬性值未包含在雙引號(")之中時,遇到如下異常:
<?xml version="1.0"?> <note id=hello> </note>
bad.xml中非法符號,在"if salary < 1000 then"語句的‘<',如下:
<?xml version="1.0"?> <note id="hello"> if salary < 1000 then </note
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 2, column 9
以上就是python標準庫ElementTree處理xml的詳細內(nèi)容,更多關于python ElementTree處理xml的資料請關注腳本之家其它相關文章!
相關文章
解決Pycharm下面出現(xiàn)No R interpreter defined的問題
今天小編就為大家分享一篇解決Pycharm下面出現(xiàn)No R interpreter defined的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Python3+Appium實現(xiàn)多臺移動設備操作的方法
這篇文章主要介紹了Python3+Appium實現(xiàn)多臺移動設備操作的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07
在Python函數(shù)中輸入任意數(shù)量參數(shù)的實例
今天小編就為大家分享一篇在Python函數(shù)中輸入任意數(shù)量參數(shù)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
python 將有序數(shù)組轉(zhuǎn)換為二叉樹的方法
這篇文章主要介紹了python 將有序數(shù)組轉(zhuǎn)換為二叉樹的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-03-03
Windows+Anaconda3+PyTorch+PyCharm的安裝教程圖文詳解
這篇文章主要介紹了Windows+Anaconda3+PyTorch+PyCharm的安裝教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
如何通過神經(jīng)網(wǎng)絡實現(xiàn)線性回歸的擬合
這篇文章主要介紹了如何通過神經(jīng)網(wǎng)絡實現(xiàn)線性回歸的擬合問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05

