python標(biāo)準(zhǔn)庫(kù)ElementTree處理xml
1. 示例用法
參照官方文檔,創(chuàng)建country_data.xml測(cè)試文檔,內(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,返回一個(gè)Element對(duì)象列表。
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')
獲得一個(gè)ElementTree對(duì)象,也可以使用
tree = ElementTree.parse('country_data.xml')
Element對(duì)象具有如下屬性和操作
| elem.tag | 這個(gè)Element對(duì)象的名字(tag) |
| elem.text | 文檔內(nèi)容 |
| elem.attrib | 屬性值字典 |
| elem.tail | 與屬性一起存儲(chǔ)的其他數(shù)據(jù) |
elem[n] 返回elem的第n個(gè)子元素
elem[n] = new_elem 將elem的第n個(gè)子元素更改為不同的元素new_elem
del elem[n] 刪除子元素
len(elem) 子元素的數(shù)量
elem.find(path)
elem.getchildren() 按文檔順序返回所有子元素
elem.items()將所有元素的屬性值以(name, value)對(duì)列表形式返回
遇到非法格式的xml
ExpatError: no element found
bad.xml為空文檔時(shí),內(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中找不到對(duì)應(yīng)結(jié)束標(biāo)記符時(shí),內(nèi)容如下:
<?xml version="1.0"?> <note> </Note>
因?yàn)閰^(qū)分大小寫,所以</Note> 不能作為<note>的結(jié)束標(biāo)記。
xml.parsers.expat.ExpatError: mismatched tag: line 3, column 2
ExpatError: not well-formed(invalid token)
bad.xml中屬性值未包含在雙引號(hào)(")之中時(shí),遇到如下異常:
<?xml version="1.0"?> <note id=hello> </note>
bad.xml中非法符號(hào),在"if salary < 1000 then"語(yǔ)句的‘<',如下:
<?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標(biāo)準(zhǔn)庫(kù)ElementTree處理xml的詳細(xì)內(nèi)容,更多關(guān)于python ElementTree處理xml的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決Pycharm下面出現(xiàn)No R interpreter defined的問(wèn)題
今天小編就為大家分享一篇解決Pycharm下面出現(xiàn)No R interpreter defined的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
Python3+Appium實(shí)現(xiàn)多臺(tái)移動(dòng)設(shè)備操作的方法
這篇文章主要介紹了Python3+Appium實(shí)現(xiàn)多臺(tái)移動(dòng)設(shè)備操作的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
在Python函數(shù)中輸入任意數(shù)量參數(shù)的實(shí)例
今天小編就為大家分享一篇在Python函數(shù)中輸入任意數(shù)量參數(shù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
python 將有序數(shù)組轉(zhuǎn)換為二叉樹的方法
這篇文章主要介紹了python 將有序數(shù)組轉(zhuǎn)換為二叉樹的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Windows+Anaconda3+PyTorch+PyCharm的安裝教程圖文詳解
這篇文章主要介紹了Windows+Anaconda3+PyTorch+PyCharm的安裝教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
如何通過(guò)神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)線性回歸的擬合
這篇文章主要介紹了如何通過(guò)神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)線性回歸的擬合問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05

