python读写xml文件实例详解嘛
作者:单单一个越字 时间:2023-03-29 13:04:33
xml文件:country.xml
<data>
<country name="shdi2hajk">231
<rank>1<NewNode A="1">This is NEW</NewNode></rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor direction="N" name="Malaysia" />
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor direction="W" name="Costa Rica" />
<neighbor direction="E" name="Colombia" />
</country>
<MediaPlatformService height="165" ip="36.32.160.199" passWord="111" port="9084" userName="admin" width="220">
</MediaPlatformService>
</data>
xml文件解读
1.xml一个节点有三个属性:tag、text、attrib
2. 以第一个子节点country为例:
3. tag代表节点名字,country节点的tag就是它的名字:country
4. text代表节点文本内容,rank节点的text就是1
5. attrib代表节点包含的属性,以{属性:值}这样的字典形式存放。country节点的属性是{name:Liechtenstein}.name是属性的键,Liechtenstein是属性的值。{属性:值}就是一个字典类型,可以使用一切字典方法。
6. country节点的tag为country,attrib为{name:Liechtenstein},text为空
7. rank节点的tag为rank,attrib为空字典,text为1
8. 综上所述,xml文档主要由节点以及节点的三个属性组成。
读取文件:
import xml.etree.ElementTree as ET
file_path = r'xml_te.xml'
tree = ET.ElementTree(file = file_path) #读取xml文件
print(tree.iter())
for i in tree.iter('rank'): #迭代获取tag为'rank'的节点
print(i.text)
nodes = tree.find('country') #获取第一个tag为country的节点,返回是子节点的迭代对象
print(nodes.tag)
nodes2 = tree.findall('country') #获取所有tag为country的节点
print(nodes2)
for node in nodes2:
#打印节点的三个属性
print(node.tag)
print(node.attrib)
print(node.text)
增加新节点及修改属性值和文本
import xml.etree.ElementTree as ET
file_path = r'xml_te.xml'
tree = ET.ElementTree(file = file_path) #读取xml文件
# root = tree.getroot() #获取根结点
"""增加新节点"""
net = ET.Element('NewNode')
net.attrib = {'A':"1"} #节点属性
net.text = "This is NEW" #节点文本
node = tree.find('country/rank/NewNode') #找到需要增加子节点的父节点
node.append(net)
print(node.text)
tree.write(file_path) #写入文件
"""修改属性值"""
sub = tree.find('country') #找到节点
sub.set('name',"shdi2hajk") #set(key,new value)
sub.text = '231'
print(sub.attrib)
print(sub.text)
tree.write(file_path) #写入文件
来源:https://blog.csdn.net/qq_38122800/article/details/123208245
标签:python,读写,xml,文件


猜你喜欢
vue使用v-if v-show页面闪烁,div闪现的解决方法
2024-04-28 09:31:49
被jQuery折腾得半死,揭秘为何jQuery为何在IE/Firefox下均无法使用
2024-05-11 09:33:27
深入理解TCP协议与UDP协议的原理及区别
2022-11-06 21:30:31

简易CSS相册源代码
2008-04-18 12:28:00
SQL脚本语言学习(黑客篇)
2008-02-29 13:09:00
PHP session 会话处理函数
2023-11-15 14:55:53
Python实现8种常用抽样方法
2023-02-01 18:03:19

pytorch 如何使用batch训练lstm网络
2023-10-18 04:46:02

详解Python中的条件判断语句
2022-05-03 09:33:28

一个ASP(VBScript)简单SQL语句构建“类”
2008-03-12 07:08:00
Python机器学习之AdaBoost算法
2021-03-13 08:07:18

MySQL的表级锁,行级锁,排它锁和共享锁
2024-01-28 15:44:22

Python如何批量生成和调用变量
2023-11-27 21:21:08
python正则表达式之对号入座篇
2021-03-31 17:59:55

详解python 降级到3.6终极解决方案
2022-07-07 20:50:26
基于Python中的turtle绘画星星和星空
2022-10-31 08:57:41

Python xlrd excel文件操作代码实例
2021-05-19 21:52:18

FSO无效的过程调用或参数问题
2010-03-25 21:49:00
MySQL索引概念及七种索引类型分享介绍
2024-01-23 03:55:27
pyinstaller打包django项目的实现步骤
2022-08-17 14:28:15
