学习笔记-模块之xml文件处理

处理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'):  #遍历country
5     rank = int(country.find('rank').text)  #找出rank
6     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)  # 打印生成的格式

猜你喜欢

转载自www.cnblogs.com/zhong2018/p/8974185.html