基于Python的XML格式的文件示例代碼詳解
XML文件是可拓展標記語言,是一種簡單的數(shù)據(jù)存儲語言,被設計用來傳輸和存儲數(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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python2.x中str與unicode相關(guān)問題的解決方法
這篇文章主要介紹了Python2.x中str與Unicode相關(guān)問題的解決方法,Python2.x版本中由于沒有默認使用Unicode而會在實際使用中碰到一些字符問題,針對這些問題本文討論了一些解決方法,需要的朋友可以參考下2015-03-03
Pandas告警UserWarning:pandas?only?supports?SQLAlchemy?conn
這篇文章主要給大家介紹了關(guān)于Pandas告警UserWarning:pandas only supports SQLAlchemy connectable的處理方式,文中還分享了pandas還有哪些userwarning,對大家學習或者工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-02-02
講解Python3中NumPy數(shù)組尋找特定元素下標的兩種方法
這篇文章主要介紹了講解Python3中NumPy數(shù)組尋找特定元素下標的兩種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08
python實現(xiàn)ssh及sftp功能(實例代碼)
這篇文章主要介紹了python實現(xiàn)ssh及sftp功能 ,本文分步驟通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03

