博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
学习笔记-模块之xml文件处理
阅读量:4326 次
发布时间:2019-06-06

本文共 1871 字,大约阅读时间需要 6 分钟。

处理XML文档
遍历xml文档
1 #遍历xml文档的子标签 2 for child in root: 3     print(child.tag,child.attrib) 4              #子标签名       属性 5     for i in child: 6         print(i.tag,i.text) 7              #子标签名   属性 8              #字节数标签不能遍历出属性,输出为空 9         #改进10         print(i.tag,i.text,i.attrib)11 12 #遍历自己需要的 例如只遍历'year'节点13 for node in root.iter('year'):14     print(node.tag,node.text)
修改
1 import xml.etree.ElementTree as ET 2  3 tree=ET.parse('xml_test.xml')   4 root=tree.getroot() 5  6 for node in root.iter('year'): 7     new_year=int(node.text)+1 8     node.text=str(new_year) 9         #赋值10     node.set('updated','yes')11           给year标签增加属性12 13 tree.write('xml_test.xml')
删除node
1 tree=ET.parse('xml_test.xml')  2 root=tree.getroot()3 4 for country in root.findall('country'):  #遍历country5     rank = int(country.find('rank').text)  #找出rank6     if rank > 50:7         root.remove(country)8 9 tree.write('output.xml') #写入文件为output.xml文件中

 

新建xml文件

1 import xml.etree.ElementTree as ET 2  3 new_xml = ET.Element("personinfolist")  #创建根节点 4 personinfo = ET.SubElement(new_xml, "personinfo", attrib={
"enrolled": "yes"}) 5 #是new_xml的子节点 6 name = ET.SubElement(personinfo, "name") 7 name.text='Jeck' 8 age = ET.SubElement(personinfo, "age", attrib={
"checked": "no"}) 9 #是personnifo的子节点10 sex = ET.SubElement(personinfo, "sex")11 sex.text='Men'12 age.text = '33' #给age节点赋值13 personinfo2 = ET.SubElement(new_xml, "personinfo", attrib={
"enrolled": "no"})14 name = ET.SubElement(personinfo2, "name")15 name.text='Ailice'16 age = ET.SubElement(personinfo2, "age")17 age.text = '19'18 19 et = ET.ElementTree(new_xml) # 生成文档对象20 et.write("test.xml", encoding="utf-8", xml_declaration=True)21 #写入到test.xml文件中 声明是xml格式的22 23 ET.dump(new_xml) # 打印生成的格式

 

转载于:https://www.cnblogs.com/zhong2018/p/8974185.html

你可能感兴趣的文章
linear-gradient常用实现效果
查看>>
sql语言的一大类 DML 数据的操纵语言
查看>>
VMware黑屏解决方法
查看>>
JS中各种跳转解析
查看>>
JAVA 基础 / 第八课:面向对象 / JAVA类的方法与实例方法
查看>>
Ecust OJ
查看>>
P3384 【模板】树链剖分
查看>>
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
Equivalent Strings
查看>>
flume handler
查看>>
收藏其他博客园主写的代码,学习加自用。先表示感谢!!!
查看>>
H5 表单标签
查看>>
su 与 su - 区别
查看>>
C语言编程-9_4 字符统计
查看>>
在webconfig中写好连接后,在程序中如何调用?
查看>>
限制用户不能删除SharePoint列表中的条目(项目)
查看>>
【Linux网络编程】使用GDB调试程序
查看>>
feign调用spring clound eureka 注册中心服务
查看>>