python解析xml文件并修改其屬性值方式
python解析xml文件并修改其屬性值
本文章主要介紹了python 解析xml文件,修改其屬性,并且進(jìn)行回寫的過程,博主目前發(fā)現(xiàn)有兩種方式進(jìn)行實(shí)現(xiàn);但是兩種實(shí)現(xiàn)有如下區(qū)別:
- 采用 python自帶的包 xml.etree.ElementTree 進(jìn)行xml解析和回寫,無法給xml文件添加standalone=‘yes’ 屬性
- 采用 第三方包 lxml 進(jìn)行xml解析和回寫,可以在xml文件添加standalone=‘yes’ 屬性
詳細(xì)的區(qū)別請(qǐng)看下面的內(nèi)容
一. 采用 python 自帶包進(jìn)行xml解析
實(shí)現(xiàn)給textCompent增加一個(gè)屬性 status= development
- xml 文件的內(nèi)容如下
<?xml version='1.0' encoding='utf-8'?> <config> <set_status>development</set_status> <component> <componentElement name="occN"> <textCompent name="apk1" status="online" /> <textCompent name="apk2" status="online" /> <textCompent name="apk3" status="online" /> <textCompent name="apk4" status="online" /> <textCompent name="apk5" status="online" /> </componentElement> <componentElement name="occK"> <textCompent name="apk1" status="online" /> <textCompent name="apk2" status="online" /> <textCompent name="apk3" status="online" /> <textCompent name="apk4" status="online" /> <textCompent name="apk5" status="online" /> </componentElement> </component> </config>
- 修改后的xml文件
<?xml version='1.0' encoding='UTF-8' standalone='no'?> <config> <set_status>development</set_status> <component> <componentElement name="occN"> <textCompent name="apk1" status="development"/> <textCompent name="apk2" status="development"/> <textCompent name="apk3" status="development"/> <textCompent name="apk4" status="development"/> <textCompent name="apk5" status="development"/> </componentElement> <componentElement name="occK"> <textCompent name="apk1" status="development"/> <textCompent name="apk2" status="development"/> <textCompent name="apk3" status="development"/> <textCompent name="apk4" status="development"/> <textCompent name="apk5" status="development"/> </componentElement> </component> </config>
- python script xml 處理
import xml.etree.ElementTree as ET config_file = "demol.xml" def change_xml(): # 解析xml文件成一個(gè)tree tree = ET.parse(config_file) # 獲取根節(jié)點(diǎn),<config>下面的內(nèi)容 root = tree.getroot() global status_set #獲取嵌入數(shù)據(jù),例如 <set_status>development</set_status> status_set = root.find("set_status").text component_list = root.find("component") for component in component_list.iter('componentElement'): for textCompent in component.iter('textCompent'): # 獲取目前的屬性值,例如 <textCompent name="apk5" status="online" /> status=textCompent.attrib.get("status") # 設(shè)置新的屬性值 textCompent.set("status",status_set) # xml_declaration 表示帶有文件頭,例如 <?xml version='1.0' encoding='utf-8'?> # 此處寫文件的時(shí)候無法設(shè)置standalone屬性,否則會(huì)報(bào)錯(cuò) tree.write("demol.xml", encoding="utf-8",xml_declaration=True) if __name__ == '__main__': change_xml()
二. 采用第三方包lxml進(jìn)行xml的操作
實(shí)現(xiàn)給textCompent增加一個(gè)屬性 status= development
- 下載 lxml包
pip install lxml
- python script xml 處理:
目前發(fā)現(xiàn)的寫法區(qū)別只有:在解析xml文件和回寫xml文件時(shí)有不同,其他相同
from lxml import etree config_file = "demol.xml" def change_xml(): # 解析xml文件成一個(gè)tree tree = etree.parse(config_file) # 獲取根節(jié)點(diǎn),<config>下面的內(nèi)容 root = tree.getroot() global status_set #獲取嵌入數(shù)據(jù),例如 <set_status>development</set_status> status_set = root.find("set_status").text component_list = root.find("component") for component in component_list.iter('componentElement'): for textCompent in component.iter('textCompent'): # 獲取目前的屬性值,例如 <textCompent name="apk5" status="online" /> status=textCompent.attrib.get("status") # 設(shè)置新的屬性值 textCompent.set("status",status_set) # xml_declaration 表示帶有文件頭,例如 <?xml version='1.0' encoding='utf-8'?> # 此處寫文件的時(shí)候可以standalone屬性 tree.write("demol.xml", xml_declaration=True, pretty_print=True, encoding=tree.docinfo.encoding, standalone=tree.docinfo.standalone) if __name__ == '__main__': change_xml()
- 修改后的xml文件
<?xml version='1.0' encoding='UTF-8' standalone='no'?> <config> <set_status>development</set_status> <component> <componentElement name="occN"> <textCompent name="apk1" status="development"/> <textCompent name="apk2" status="development"/> <textCompent name="apk3" status="development"/> <textCompent name="apk4" status="development"/> <textCompent name="apk5" status="development"/> </componentElement> <componentElement name="occK"> <textCompent name="apk1" status="development"/> <textCompent name="apk2" status="development"/> <textCompent name="apk3" status="development"/> <textCompent name="apk4" status="development"/> <textCompent name="apk5" status="development"/> </componentElement> </component> </config>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python基礎(chǔ)進(jìn)階之海量表情包多線程爬蟲功能的實(shí)現(xiàn)
這篇文章主要介紹了Python基礎(chǔ)進(jìn)階之海量表情包多線程爬蟲,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12Python字符串對(duì)齊、刪除字符串不需要的內(nèi)容以及格式化打印字符
這篇文章主要給大家介紹了關(guān)于Python字符串對(duì)齊、刪除字符串不需要的內(nèi)容以及格式化打印字符的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01python關(guān)于矩陣重復(fù)賦值覆蓋問題的解決方法
這篇文章主要介紹了python關(guān)于矩陣重復(fù)賦值覆蓋問題的解決方法,涉及Python深拷貝與淺拷貝相關(guān)操作與使用技巧,需要的朋友可以參考下2019-07-071 行 Python 代碼快速實(shí)現(xiàn) FTP 服務(wù)器
FTP 服務(wù)器,在此之前我都是使用Linux的vsftpd軟件包來搭建FTP服務(wù)器的,現(xiàn)在發(fā)現(xiàn)了利用pyftpdlib可以更加簡(jiǎn)單的方法即可實(shí)現(xiàn)FTP服務(wù)器的功能。下面小編給大家?guī)砹? 行 Python 代碼快速實(shí)現(xiàn) FTP 服務(wù)器,需要的朋友參考下2018-01-01opencv 實(shí)現(xiàn)特定顏色線條提取與定位操作
這篇文章主要介紹了opencv 實(shí)現(xiàn)特定顏色線條提取與定位操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06python識(shí)別圖像并提取文字的實(shí)現(xiàn)方法
這篇文章主要介紹了python識(shí)別圖像并提取文字的實(shí)現(xiàn)方法,2019-06-06Python自然語言處理庫(kù)之NLTK庫(kù)初級(jí)教程
NLTK(Natural Language Toolkit)是一個(gè)Python庫(kù),用于實(shí)現(xiàn)自然語言處理(NLP)的許多任務(wù),NLTK包括一些有用的工具和資源,如文本語料庫(kù)、詞性標(biāo)注器、語法分析器等,在這篇初級(jí)教程中,我們將了解NLTK的基礎(chǔ)功能,需要的朋友可以參考下2023-08-08在Django中動(dòng)態(tài)地過濾查詢集的實(shí)現(xiàn)
本文主要介紹了Django中動(dòng)態(tài)地過濾查詢集的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03