欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實(shí)現(xiàn)修改xml文件內(nèi)容

 更新時(shí)間:2022年07月26日 11:06:46   作者:公號(hào)運(yùn)維家???????  
這篇文章主要介紹了python實(shí)現(xiàn)修改xml文件內(nèi)容,XML 指可擴(kuò)展標(biāo)記語(yǔ)言,是一種標(biāo)記語(yǔ)言,是從標(biāo)準(zhǔn)通用標(biāo)記語(yǔ)言(SGML)中簡(jiǎn)化修改出來(lái)的

XML 被設(shè)計(jì)用來(lái)傳輸和存儲(chǔ)數(shù)據(jù)。

HTML 被設(shè)計(jì)用來(lái)顯示數(shù)據(jù)。

XML 指可擴(kuò)展標(biāo)記語(yǔ)言(eXtensible Markup Language)。

可擴(kuò)展標(biāo)記語(yǔ)言(英語(yǔ):Extensible Markup Language,簡(jiǎn)稱:XML)是一種標(biāo)記語(yǔ)言,是從標(biāo)準(zhǔn)通用標(biāo)記語(yǔ)言(SGML)中簡(jiǎn)化修改出來(lái)的。它主要用到的有可擴(kuò)展標(biāo)記語(yǔ)言、可擴(kuò)展樣式語(yǔ)言(XSL)、XBRL和XPath等。

直接上代碼,拿來(lái)就可用。

首先需要準(zhǔn)備一個(gè)測(cè)試??xml???文件,我這個(gè)文件名字為??text.xml??;

<data>
<country name="Liechtenstein">
<rank>yunweijia</rank>
<year>2022</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E" />
<neighbor name="Switzerland" direction="W" />
</country>
<country name="Singapore">
<rank>yunweijia</rank>
<year>2023</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N" />
</country>
<country name="Panama">
<rank>yunweijia</rank>
<year>2024</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W" />
<neighbor name="Colombia" direction="E" />
</country>
</data>

然后使用以下代碼來(lái)進(jìn)行修改;

import xml.etree.ElementTree as ET
def change_one_xml(xml_path, xml_dw, update_content):
# 打開xml文檔
doc = ET.parse(xml_path)
root = doc.getroot()
# 查找修改路勁
sub1 = root.find(xml_dw)
# 修改標(biāo)簽內(nèi)容
sub1.text = update_content
# 保存修改
doc.write(xml_path)

# 欲修改文件
xml_path = r'test.xml'

# 修改文件中的xpath定位
xml_dw = './/country[@name="Singapore"]/year'

# 想要修改成什么內(nèi)容
update_content = '9999'
change_one_xml(xml_path, xml_dw, update_content)

運(yùn)行完畢之后,我們可以看到源文件內(nèi)容變成了;

<data>
<country name="Liechtenstein">
<rank>yunweijia</rank>
<year>2022</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E" />
<neighbor name="Switzerland" direction="W" />
</country>
<country name="Singapore">
<rank>yunweijia</rank>
<year>9999</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N" />
</country>
<country name="Panama">
<rank>yunweijia</rank>
<year>2024</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W" />
<neighbor name="Colombia" direction="E" />
</country>
</data>

到此這篇關(guān)于python實(shí)現(xiàn)修改xml文件內(nèi)容的文章就介紹到這了,更多相關(guān)python修改xml文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論