最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python解析xml文件并修改其屬性值方式

 更新時(shí)間:2025年02月19日 15:55:28   作者:愛(ài)吃面條的小白  
本文介紹了兩種解析和修改XML文件的方法:使用Python自帶的xml.etree.ElementTree和第三方的lxml,lxml方法可以添加standalone屬性,而ElementTree則不能

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ā)布

    使用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
  • Python使用MONGODB入門(mén)實(shí)例

    Python使用MONGODB入門(mén)實(shí)例

    這篇文章主要介紹了Python使用MONGODB的方法,實(shí)例分析了Python使用MONGODB的啟動(dòng)、安裝及使用的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • Python3中bytes類(lèi)型轉(zhuǎn)換為str類(lèi)型

    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ù)流程介紹

    這篇文章主要介紹了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)

    本文主要介紹了Python Excel 通用篩選函數(shù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-08-08
  • 在Python中表示一個(gè)對(duì)象的方法

    在Python中表示一個(gè)對(duì)象的方法

    這篇文章主要介紹了在Python中表示一個(gè)對(duì)象的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-06-06
  • 使用python實(shí)現(xiàn)正則匹配檢索遠(yuǎn)端FTP目錄下的文件

    使用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)題

    這篇文章主要介紹了python轉(zhuǎn)換pkl模型文件為txt文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Python的Flask框架應(yīng)用調(diào)用Redis隊(duì)列數(shù)據(jù)的方法

    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
  • Python自定義迭代器的實(shí)現(xiàn)方法

    Python自定義迭代器的實(shí)現(xiàn)方法

    自定義迭代器的核心是嚴(yán)格遵守 Python 迭代器協(xié)議,只要一個(gè)類(lèi)實(shí)現(xiàn)了__iter__()和__next__()這兩個(gè)特殊方法,它的實(shí)例就是一個(gè)合法的迭代器,本文給大家介紹了Python自定義迭代器的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2026-05-05

最新評(píng)論

奉新县| 保德县| 咸阳市| 兴和县| 炉霍县| 肇源县| 清水河县| 晋城| 法库县| 十堰市| 那坡县| 河东区| 峨边| 聂拉木县| 巴里| 阿坝| 凌海市| 色达县| 望江县| 嵊州市| 句容市| 中方县| 庆云县| 贵定县| 满洲里市| 冕宁县| 乌拉特前旗| 东山县| 新蔡县| 奉节县| 灵石县| 前郭尔| 瑞安市| 安国市| 崇阳县| 句容市| 徐汇区| 富源县| 昌吉市| 桦川县| 蒙阴县|