基于Python的XML格式的文件示例代码详解
XML文件是可拓展标记语言,是一种简单的数据存储语言,被设计用来传输和存储数据
在Python中XML的一些方法
读取文件和内容
#引用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 ETcontent = '''<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>
读取节点数据
from xml.etree import ElementTree as ETcontent = '''<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>'''# 获取根标签 dataroot = 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标签里面的内容
from xml.etree import ElementTree as ETcontent = '''<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>'''# 获取根标签 dataroot = 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 ETcontent = '''<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 ETcontent = '''<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)
删除和修改节点
from xml.etree import ElementTree as ETcontent = '''<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)# 修改节点内容和属性rank = root.find(’country’).find(’rank’)print(rank.text)rank.text = '999' # 修改rank标签里面的内容rank.set(’update’, ’2020-11-11’) # 为rank标签新增一个update属性print(rank.text, rank.attrib)############ 保存文件 ############tree = ET.ElementTree(root)tree.write('new.xml', encoding=’utf-8’)# 删除节点root.remove( root.find(’country’) )print(root.findall(’country’))############ 保存文件 ############tree = ET.ElementTree(root)tree.write('newnew.xml', encoding=’utf-8’)
构建文档
<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# 创建根标签root = ET.Element('home')# 创建节点大儿子son1 = ET.Element(’son’, {’name’: ’儿1’})# 创建小儿子son2 = ET.Element(’son’, {'name': ’儿2’})# 在大儿子中创建两个孙子grandson1 = ET.Element(’grandson’, {’name’: ’儿11’})grandson2 = ET.Element(’grandson’, {’name’: ’儿12’})son1.append(grandson1)son1.append(grandson2)# 把儿子添加到根节点中root.append(son1)root.append(son2)tree = ET.ElementTree(root)tree.write(’oooo.xml’, encoding=’utf-8’, short_empty_elements=False) #short_empty_elements 是否采取短标签的形式创建
<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# 创建根节点root = ET.Element('famliy')# 创建大儿子son1 = root.makeelement(’son’, {’name’: ’儿1’})# 创建小儿子son2 = root.makeelement(’son’, {'name': ’儿2’})# 在大儿子中创建两个孙子grandson1 = son1.makeelement(’grandson’, {’name’: ’儿11’})grandson2 = son1.makeelement(’grandson’, {’name’: ’儿12’})son1.append(grandson1)son1.append(grandson2)# 把儿子添加到根节点中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# 创建根节点root = ET.Element('famliy')# 创建节点大儿子son1 = ET.SubElement(root, 'son', attrib={’name’: ’儿1’})# 创建小儿子son2 = ET.SubElement(root, 'son', attrib={'name': '儿2'})# 在大儿子中创建一个孙子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# 创建根节点root = ET.Element('user')root.text = '<![CDATA[你好呀]]'et = ET.ElementTree(root) # 生成文档对象et.write('test.xml', encoding='utf-8')
到此这篇关于基于Python的XML格式的文件的文章就介绍到这了,更多相关python xml格式文件内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!
相关文章: