python解析xml文件并修改其屬性值方式
python解析xml文件并修改其屬性值
本文章主要介紹了python 解析xml文件,修改其屬性,并且進(jìn)行回寫(xiě)的過(guò)程,博主目前發(fā)現(xiàn)有兩種方式進(jìn)行實(shí)現(xiàn);但是兩種實(shí)現(xiàn)有如下區(qū)別:
- 采用 python自帶的包 xml.etree.ElementTree 進(jìn)行xml解析和回寫(xiě),無(wú)法給xml文件添加standalone=‘yes’ 屬性
- 采用 第三方包 lxml 進(jìn)行xml解析和回寫(xiě),可以在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'?>
# 此處寫(xiě)文件的時(shí)候無(wú)法設(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)的寫(xiě)法區(qū)別只有:在解析xml文件和回寫(xiě)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'?>
# 此處寫(xiě)文件的時(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實(shí)現(xiàn)Flutter項(xiàng)目的自動(dòng)化構(gòu)建與發(fā)布
本文介紹了作者為解決移動(dòng)端Flutter應(yīng)用開(kāi)發(fā)中構(gòu)建、上傳和通知的繁瑣問(wèn)題,使用Python編寫(xiě)的一套自動(dòng)化腳本,該腳本實(shí)現(xiàn)了自動(dòng)構(gòu)建、上傳到蒲公英測(cè)試平臺(tái)、發(fā)送郵件通知等功能,顯著提高了開(kāi)發(fā)效率,需要的朋友可以參考下2026-02-02
Python3中bytes類(lèi)型轉(zhuǎn)換為str類(lèi)型
Python 3最重要的新特性之一是對(duì)字符串和二進(jìn)制數(shù)據(jù)流做了明確的區(qū)分。這篇文章主要介紹了Python3中bytes類(lèi)型轉(zhuǎn)換為str類(lèi)型的相關(guān)知識(shí),需要的朋友可以參考下2018-09-09
python flask sqlalchemy連接數(shù)據(jù)庫(kù)流程介紹
這篇文章主要介紹了python flask sqlalchemy連接數(shù)據(jù)庫(kù)流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09
Python Excel 通用篩選函數(shù)的實(shí)現(xiàn)
本文主要介紹了Python Excel 通用篩選函數(shù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-08-08
使用python實(shí)現(xiàn)正則匹配檢索遠(yuǎn)端FTP目錄下的文件
這篇文章主要介紹了使用python實(shí)現(xiàn)正則匹配檢索遠(yuǎn)端FTP目錄下的文件的方法,非常的簡(jiǎn)單實(shí)用,需要的小伙伴參考下2015-03-03
python轉(zhuǎn)換pkl模型文件為txt文件問(wèn)題
這篇文章主要介紹了python轉(zhuǎn)換pkl模型文件為txt文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Python的Flask框架應(yīng)用調(diào)用Redis隊(duì)列數(shù)據(jù)的方法
這里為大家?guī)?lái)Python的Flask框架應(yīng)用調(diào)用Redis隊(duì)列數(shù)據(jù)的方法,從而能夠?qū)崿F(xiàn)異步無(wú)阻塞從而提高某些實(shí)時(shí)處理情況下程序的性能,需要的朋友可以參考下2016-06-06

