基于Python的XML格式的文件示例代碼詳解
XML文件是可拓展標(biāo)記語言,是一種簡(jiǎn)單的數(shù)據(jù)存儲(chǔ)語言,被設(shè)計(jì)用來傳輸和存儲(chǔ)數(shù)據(jù)
在Python中XML的一些方法
讀取文件和內(nèi)容
#引用xml模塊 from xml.etree import ElementTree as ET # ET去打開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屬性(沒有屬性為:{}) 獲取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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python內(nèi)建類型int源碼學(xué)習(xí)
這篇文章主要為大家介紹了Python內(nèi)建類型int源碼學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05十一個(gè)案例帶你吃透Python函數(shù)參數(shù)
這篇文章主要通過十一個(gè)案例帶大家一起了解一下Python中的函數(shù)參數(shù),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定幫助,需要的可以參考一下2022-08-08Python2.x中str與unicode相關(guān)問題的解決方法
這篇文章主要介紹了Python2.x中str與Unicode相關(guān)問題的解決方法,Python2.x版本中由于沒有默認(rèn)使用Unicode而會(huì)在實(shí)際使用中碰到一些字符問題,針對(duì)這些問題本文討論了一些解決方法,需要的朋友可以參考下2015-03-03Pandas告警UserWarning:pandas?only?supports?SQLAlchemy?conn
這篇文章主要給大家介紹了關(guān)于Pandas告警UserWarning:pandas only supports SQLAlchemy connectable的處理方式,文中還分享了pandas還有哪些userwarning,對(duì)大家學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-02-02講解Python3中NumPy數(shù)組尋找特定元素下標(biāo)的兩種方法
這篇文章主要介紹了講解Python3中NumPy數(shù)組尋找特定元素下標(biāo)的兩種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08python實(shí)現(xiàn)ssh及sftp功能(實(shí)例代碼)
這篇文章主要介紹了python實(shí)現(xiàn)ssh及sftp功能 ,本文分步驟通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03