主頁 > 知識庫 > 基于Python的XML格式的文件示例代碼詳解

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

熱門標簽:長春人工智能電銷機器人官網(wǎng) ai地圖標注 地圖標注推廣單頁 廈門crm外呼系統(tǒng)如何 百應ai電銷機器人鄭州 女王谷地圖標注 西藏快速地圖標注地點 n400電話申請多少錢 如何在地圖標注文字

XML文件是可拓展標記語言,是一種簡單的數(shù)據(jù)存儲語言,被設(shè)計用來傳輸和存儲數(shù)據(jù)

在Python中XML的一些方法

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

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

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

# 獲取根標簽
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) # 獲取根標簽 
print(root) # Element 'data' at 0x7fdaa019cea0>

讀取節(jié)點數(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>
"""

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

country_object = root.find("country") # 獲取XML文件中的country標簽
print(country_object.tag, country_object.attrib)# 獲取country標簽名  獲取country標簽地屬性
gdppc_object = country_object.find("gdppc")# 獲取gdppc標簽
print(gdppc_object.tag,gdppc_object.attrib,gdppc_object.text)# 獲取gdppc標簽的名稱  獲取gdppc屬性(沒有屬性為:{}) 獲取gdppc標簽里面的內(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>
"""

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

# 獲取data標簽的孩子標簽
for child in root:
  # child.tag = conntry 獲取到兩個country標簽
  # child.attrib = {"name":"Liechtenstein"}
  print(child.tag, child.attrib)
  for node in child:
    print(node.tag, node.attrib, node.text) # 獲取到reank標簽
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標簽
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標簽
print(v1)

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

刪除和修改節(jié)點

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é)點內(nèi)容和屬性
rank = root.find('country').find('rank')
print(rank.text)
rank.text = "999" # 修改rank標簽里面的內(nèi)容
rank.set('update', '2020-11-11') # 為rank標簽新增一個update屬性
print(rank.text, rank.attrib)
############ 保存文件 ############
tree = ET.ElementTree(root)
tree.write("new.xml", encoding='utf-8')

# 刪除節(jié)點
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)建根標簽
root = ET.Element("home")

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

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

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

tree = ET.ElementTree(root)
tree.write('oooo.xml', encoding='utf-8', short_empty_elements=False) #short_empty_elements 是否采取短標簽的形式創(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é)點
root = ET.Element("famliy")


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

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

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


# 把兒子添加到根節(jié)點中
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é)點
root = ET.Element("famliy")


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

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


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

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

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

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

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

標簽:渭南 綿陽 興安盟 內(nèi)江 拉薩 廊坊 黔東 亳州

巨人網(wǎng)絡(luò)通訊聲明:本文標題《基于Python的XML格式的文件示例代碼詳解》,本文關(guān)鍵詞  基于,Python,的,XML,格式,文件,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《基于Python的XML格式的文件示例代碼詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于基于Python的XML格式的文件示例代碼詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章