主頁(yè) > 知識(shí)庫(kù) > 基于Python的XML格式的文件示例代碼詳解

基于Python的XML格式的文件示例代碼詳解

熱門(mén)標(biāo)簽:客戶服務(wù) 呼叫中心市場(chǎng)需求 企業(yè)做大做強(qiáng) 語(yǔ)音系統(tǒng) 電話運(yùn)營(yíng)中心 Win7旗艦版 百度AI接口 硅谷的囚徒呼叫中心

XML文件是可拓展標(biāo)記語(yǔ)言,是一種簡(jiǎn)單的數(shù)據(jù)存儲(chǔ)語(yǔ)言,被設(shè)計(jì)用來(lái)傳輸和存儲(chǔ)數(shù)據(jù)

在Python中XML的一些方法

讀取文件和內(nèi)容

#引用xml模塊
from xml.etree import ElementTree as ET

# ET去打開(kāi)xml文件
tree = ET.parse("files/xo.xml")

# 獲取根標(biāo)簽
root = tree.getroot()

print(root) # Element 'data' at 0x7f94e02763b0>
from xml.etree import ElementTree as ET

content = """
data>
  country name="Liechtenstein">
    rank updated="yes">2/rank>
    year>2023/year>
    gdppc>141100/gdppc>
    neighbor direction="E" name="Austria" />
    neighbor direction="W" name="Switzerland" />
  /country>
   country name="Panama">
    rank updated="yes">69/rank>
    year>2026/year>
    gdppc>13600/gdppc>
    neighbor direction="W" name="Costa Rica" />
    neighbor direction="E" name="Colombia" />
  /country>
/data>
"""

root = ET.XML(content) # 獲取根標(biāo)簽 
print(root) # Element 'data' at 0x7fdaa019cea0>

讀取節(jié)點(diǎn)數(shù)據(jù)

from xml.etree import ElementTree as ET

content = """
data>
  country name="Liechtenstein" id="999" >
    rank>2/rank>
    year>2023/year>
    gdppc>141100/gdppc>
    neighbor direction="E" name="Austria" />
    neighbor direction="W" name="Switzerland" />
  /country>
   country name="Panama">
    rank>69/rank>
    year>2026/year>
    gdppc>13600/gdppc>
    neighbor direction="W" name="Costa Rica" />
    neighbor direction="E" name="Colombia" />
  /country>
/data>
"""

# 獲取根標(biāo)簽 data
root = ET.XML(content)

country_object = root.find("country") # 獲取XML文件中的country標(biāo)簽
print(country_object.tag, country_object.attrib)# 獲取country標(biāo)簽名  獲取country標(biāo)簽地屬性
gdppc_object = country_object.find("gdppc")# 獲取gdppc標(biāo)簽
print(gdppc_object.tag,gdppc_object.attrib,gdppc_object.text)# 獲取gdppc標(biāo)簽的名稱  獲取gdppc屬性(沒(méi)有屬性為:{}) 獲取gdppc標(biāo)簽里面的內(nèi)容
from xml.etree import ElementTree as ET

content = """
data>
  country name="Liechtenstein">
    rank>2/rank>
    year>2023/year>
    gdppc>141100/gdppc>
    neighbor direction="E" name="Austria" />
    neighbor direction="W" name="Switzerland" />
  /country>
   country name="Panama">
    rank>69/rank>
    year>2026/year>
    gdppc>13600/gdppc>
    neighbor direction="W" name="Costa Rica" />
    neighbor direction="E" name="Colombia" />
  /country>
/data>
"""

# 獲取根標(biāo)簽 data
root = ET.XML(content)

# 獲取data標(biāo)簽的孩子標(biāo)簽
for child in root:
  # child.tag = conntry 獲取到兩個(gè)country標(biāo)簽
  # child.attrib = {"name":"Liechtenstein"}
  print(child.tag, child.attrib)
  for node in child:
    print(node.tag, node.attrib, node.text) # 獲取到reank標(biāo)簽
from xml.etree import ElementTree as ET

content = """
data>
  country name="Liechtenstein">
    rank>2/rank>
    year>2023/year>
    gdppc>141100/gdppc>
    neighbor direction="E" name="Austria" />
    neighbor direction="W" name="Switzerland" />
  /country>
   country name="Panama">
    rank>69/rank>
    year>2026/year>
    gdppc>13600/gdppc>
    neighbor direction="W" name="Costa Rica" />
    neighbor direction="E" name="Colombia" />
  /country>
/data>
"""

root = ET.XML(content)

# 找到子子孫孫的year標(biāo)簽
for child in root.iter('year'):
  print(child.tag, child.text)
from xml.etree import ElementTree as ET

content = """
data>
  country name="Liechtenstein">
    rank>2/rank>
    year>2023/year>
    gdppc>141100/gdppc>
    neighbor direction="E" name="Austria" />
    neighbor direction="W" name="Switzerland" />
  /country>
   country name="Panama">
    rank>69/rank>
    year>2026/year>
    gdppc>13600/gdppc>
    neighbor direction="W" name="Costa Rica" />
    neighbor direction="E" name="Colombia" />
  /country>
/data>
"""

root = ET.XML(content)
v1 = root.findall('country') # 找到所有的country標(biāo)簽
print(v1)

v2 = root.find('country').find('rank') # 找到country標(biāo)簽中的rank標(biāo)簽
print(v2.text)

刪除和修改節(jié)點(diǎn)

from xml.etree import ElementTree as ET

content = """
data>
  country name="Liechtenstein">
    rank>2/rank>
    year>2023/year>
    gdppc>141100/gdppc>
    neighbor direction="E" name="Austria" />
    neighbor direction="W" name="Switzerland" />
  /country>
   country name="Panama">
    rank>69/rank>
    year>2026/year>
    gdppc>13600/gdppc>
    neighbor direction="W" name="Costa Rica" />
    neighbor direction="E" name="Colombia" />
  /country>
/data>
"""

root = ET.XML(content)

# 修改節(jié)點(diǎn)內(nèi)容和屬性
rank = root.find('country').find('rank')
print(rank.text)
rank.text = "999" # 修改rank標(biāo)簽里面的內(nèi)容
rank.set('update', '2020-11-11') # 為rank標(biāo)簽新增一個(gè)update屬性
print(rank.text, rank.attrib)
############ 保存文件 ############
tree = ET.ElementTree(root)
tree.write("new.xml", encoding='utf-8')

# 刪除節(jié)點(diǎn)
root.remove( root.find('country') )
print(root.findall('country'))

############ 保存文件 ############
tree = ET.ElementTree(root)
tree.write("newnew.xml", encoding='utf-8')

構(gòu)建文檔

home>
  son name="兒1">
    grandson name="兒11">/grandson>
    grandson name="兒12">/grandson>
  /son>
  son name="兒2">/son>
/home>
from xml.etree import ElementTree as ET

# 創(chuàng)建根標(biāo)簽
root = ET.Element("home")

# 創(chuàng)建節(jié)點(diǎn)大兒子
son1 = ET.Element('son', {'name': '兒1'})
# 創(chuàng)建小兒子
son2 = ET.Element('son', {"name": '兒2'})

# 在大兒子中創(chuàng)建兩個(gè)孫子
grandson1 = ET.Element('grandson', {'name': '兒11'})
grandson2 = ET.Element('grandson', {'name': '兒12'})
son1.append(grandson1)
son1.append(grandson2)

# 把兒子添加到根節(jié)點(diǎn)中
root.append(son1)
root.append(son2)

tree = ET.ElementTree(root)
tree.write('oooo.xml', encoding='utf-8', short_empty_elements=False) #short_empty_elements 是否采取短標(biāo)簽的形式創(chuàng)建
famliy>
  son name="兒1">
    grandson name="兒11">/grandson>
    grandson name="兒12">/grandson>
  /son>
  son name="兒2">/son>
/famliy>
from xml.etree import ElementTree as ET

# 創(chuàng)建根節(jié)點(diǎn)
root = ET.Element("famliy")


# 創(chuàng)建大兒子
son1 = root.makeelement('son', {'name': '兒1'})
# 創(chuàng)建小兒子
son2 = root.makeelement('son', {"name": '兒2'})

# 在大兒子中創(chuàng)建兩個(gè)孫子
grandson1 = son1.makeelement('grandson', {'name': '兒11'})
grandson2 = son1.makeelement('grandson', {'name': '兒12'})

son1.append(grandson1)
son1.append(grandson2)


# 把兒子添加到根節(jié)點(diǎn)中
root.append(son1)
root.append(son2)

tree = ET.ElementTree(root)
tree.write('oooo.xml',encoding='utf-8')
famliy>
	son name="兒1">
  	age name="兒11">孫子/age>
  /son>
	son name="兒2">/son>
/famliy>
from xml.etree import ElementTree as ET


# 創(chuàng)建根節(jié)點(diǎn)
root = ET.Element("famliy")


# 創(chuàng)建節(jié)點(diǎn)大兒子
son1 = ET.SubElement(root, "son", attrib={'name': '兒1'})
# 創(chuàng)建小兒子
son2 = ET.SubElement(root, "son", attrib={"name": "兒2"})

# 在大兒子中創(chuàng)建一個(gè)孫子
grandson1 = ET.SubElement(son1, "age", attrib={'name': '兒11'})
grandson1.text = '孫子'


et = ET.ElementTree(root) #生成文檔對(duì)象
et.write("test.xml", encoding="utf-8")
user>![CDATA[你好呀]]/user>
from xml.etree import ElementTree as ET

# 創(chuàng)建根節(jié)點(diǎn)
root = ET.Element("user")
root.text = "![CDATA[你好呀]]"

et = ET.ElementTree(root) # 生成文檔對(duì)象
et.write("test.xml", encoding="utf-8")

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

您可能感興趣的文章:
  • Python大數(shù)據(jù)之使用lxml庫(kù)解析html網(wǎng)頁(yè)文件示例
  • Python3使用xml.dom.minidom和xml.etree模塊兒解析xml文件封裝函數(shù)的方法
  • python 批量修改 labelImg 生成的xml文件的方法
  • 對(duì)python修改xml文件的節(jié)點(diǎn)值方法詳解
  • Python實(shí)現(xiàn)的讀取/更改/寫(xiě)入xml文件操作示例
  • Python Xml文件添加字節(jié)屬性的方法

標(biāo)簽:海南 喀什 山西 山西 安康 長(zhǎng)沙 崇左 濟(jì)南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《基于Python的XML格式的文件示例代碼詳解》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266