python xml解析實(shí)例詳解
python xml解析
first.xml
<info> <person > <id>1</id> <name>fsy</name> <age >24</age> </person> <person> <id>2</id> <name>jianjian</name> <age>24</age> </person> <count id ='1'>1000</count> </info>
from xml.etree import ElementTree as etree
讀入
def read_xml(file): # parse()函數(shù)會(huì)返回一個(gè)能代表整篇文檔的對(duì)象。這不是根元素。要獲得根元素的引用可以調(diào)用getroot()方法。 tree = etree.parse(file) root = tree.getroot() return root
得到信息
def print_node(node):
'''''打印結(jié)點(diǎn)基本信息'''
print("node.tag:%s" % node.tag)
print("node.attrib:%s"%node.attrib)
print( "node.text:%s" % node.text)
搜索:
find_all
>>> root = read_xml ('first.xml')
>>> res = root.findall("person")
[<Element 'person' at 0x00000000033388B8>, <Element 'person' at 0x0000000003413D68>]
注意:findall只查詢直接的子節(jié)點(diǎn)
>>> r1 = root.findall("id")
>>> r1
[]
>>> r =tree.findall(".//id")
>>> for e in r:
print( e,e.text)
<Element 'id' at 0x00000000034279F8> 1
<Element 'id' at 0x0000000003427B38> 2
find:
#find()方法用來返回第一個(gè)匹配到的元素。當(dāng)我們認(rèn)為只會(huì)有一個(gè)匹配,或者有多個(gè)匹配但我們只關(guān)心第一個(gè)的時(shí)候,這個(gè)方法是很有用的。
>>> res[0].find("id")
<Element 'id' at 0x0000000003413CC8>
>>> print_node(res[0].find("id"))
node.tag:id
node.attrib:{}
node.text:1
find查找失?。?br />
使用find要注意在布爾上下文中,如果ElementTree元素對(duì)象不包含子元素,其值則會(huì)被認(rèn)為是False(即如果len(element)等于0)。這就意味著if element.find('...')并非在測試是否find()方法找到了匹配項(xiàng);這條語句是在測試匹配到的元素是否包含子元素。想要測試find()方法是否返回了一個(gè)元素,則需使用if element.find('...') is not None。
>>> bk = res[0].find("no")
>>> bk
>>> type(bk)
<class 'NoneType'>
>>> res[0].find("id")
<Element 'id' at 0x0000000003413CC8>
>>> if res[0].find("id"):
print("find")
else:
print("not find")
not find
>>> if res[0].find("id") is not None:
print("find")
else:
print("not find")
find
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Python使用lxml模塊和Requests模塊抓取HTML頁面的教程
- 在windows系統(tǒng)中實(shí)現(xiàn)python3安裝lxml
- 深入解讀Python解析XML的幾種方式
- Python實(shí)現(xiàn)將xml導(dǎo)入至excel
- 詳細(xì)解讀Python中解析XML數(shù)據(jù)的方法
- Python使用minidom讀寫xml的方法
- Python lxml模塊安裝教程
- Python中使用ElementTree解析XML示例
- python基于xmlrpc實(shí)現(xiàn)二進(jìn)制文件傳輸?shù)姆椒?/a>
- python解析xml文件實(shí)例分析
相關(guān)文章
Flask使用SocketIO實(shí)現(xiàn)WebSocket與Vue進(jìn)行實(shí)時(shí)推送
本文主要介紹了Flask使用SocketIO實(shí)現(xiàn)WebSocket與Vue進(jìn)行實(shí)時(shí)推送,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
python?tkinter庫的Text記錄點(diǎn)擊路經(jīng)和刪除記錄詳情
這篇文章主要介紹了python?tkinter庫的Text記錄點(diǎn)擊路經(jīng)和刪除記錄詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-06-06
Flask Paginate實(shí)現(xiàn)表格分頁的使用示例
flask_paginate是Flask框架的一個(gè)分頁擴(kuò)展,用于處理分頁相關(guān)的功能,本文就來介紹一下Flask Paginate實(shí)現(xiàn)表格分頁的使用示例,感興趣的可以了解一下2023-11-11
python中os.path.join()函數(shù)實(shí)例用法
在本篇文章里小編給大家整理的是一篇關(guān)于python中os.path.join()函數(shù)實(shí)例用法,有興趣的朋友們可以跟著學(xué)習(xí)參考下。2021-05-05
pytorch神經(jīng)網(wǎng)絡(luò)從零開始實(shí)現(xiàn)多層感知機(jī)
這篇文章主要為大家介紹了pytorch神經(jīng)網(wǎng)絡(luò)從零開始實(shí)現(xiàn)多層感知機(jī)的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10
Python實(shí)現(xiàn)用戶注冊(cè)登錄程序
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)用戶注冊(cè)登錄程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
對(duì)python pandas讀取剪貼板內(nèi)容的方法詳解
今天小編就為大家分享一篇對(duì)python pandas讀取剪貼板內(nèi)容的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01

