码迷,mamicode.com
首页 > 其他好文 > 详细

xml模块

时间:2018-10-04 08:54:55      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:auth   标签   iter   find   lte   name   遍历   排名   获取   

 1 # Author:Sure Feng
 2 
 3 ‘‘‘
 4 xml格式:
 5 <?xml version="1.0"?>
 6 <data>
 7     <country name="Liechtenstein">
 8         <rank updated="yes">2</rank>
 9         <year>2008</year>
10         <gdppc>141100</gdppc>
11         <neighbor name="Austria" direction="E"/>
12         <neighbor name="Switzerland" direction="W"/>
13     </country>
14     <country name="Singapore">
15         <rank updated="yes">5</rank>
16         <year>2011</year>
17         <gdppc>59900</gdppc>
18         <neighbor name="Malaysia" direction="N"/>
19     </country>
20     <country name="Panama">
21         <rank updated="yes">69</rank>
22         <year>2011</year>
23         <gdppc>13600</gdppc>
24         <neighbor name="Costa Rica" direction="W"/>
25         <neighbor name="Colombia" direction="E"/>
26     </country>
27 </data>
28 ‘‘‘
29 
30 ‘‘‘
31 IndentationError: unexpected indent:缩进错误
32 ‘‘‘
33 
34 
35 
36 # 操作xml的模块,太长了,起个别名ET
37 import xml.etree.ElementTree as ET
38 
39 tree = ET.parse("xmltest.xml")
40 # <xml.etree.ElementTree.ElementTree object at 0x0000000001DC3358>
41 print(tree)
42 # 获取Element ‘data‘对象
43 root = tree.getroot()
44 # <Element ‘data‘ at 0x0000000001DD2D68>
45 # 获取根标签
46 print(root.tag)
47 
48 # 遍历xml文档
49 for child in root:
50 # attrib以字典形式展示
51     print(child.tag, child.attrib)
52 # 获取国家排名
53     rank = int(child.find(rank).text)
54 # 删除排名50名后的国家
55     if rank > 50:
56         root.remove(child)
57     else:
58         for i in child:
59             print(i.tag, i.attrib)
60 
61 # 只遍历year节点
62 for node in root.iter(year):
63     # 把年份内容改为该大一年
64     n_text = int(node.text)
65     node.text = str(n_text + 1)
66     # 添加新属性
67     node.set("update", "yes")
68 
69 tree.write(output.xml)
70 
71 
72 
73 ‘‘‘
74 
75 import xml.etree.ElementTree as ET
76 
77 
78 new_xml = ET.Element("namelist")
79 name = ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"})
80 age = ET.SubElement(name,"age",attrib={"checked":"no"})
81 sex = ET.SubElement(name,"sex")
82 sex.text = ‘33‘
83 name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"})
84 age = ET.SubElement(name2,"age")
85 age.text = ‘19‘
86 
87 et = ET.ElementTree(new_xml) #生成文档对象
88 et.write("test.xml", encoding="utf-8",xml_declaration=True)
89 
90 ET.dump(new_xml) #打印生成的格式
91 ‘‘‘

 

xml模块

标签:auth   标签   iter   find   lte   name   遍历   排名   获取   

原文地址:https://www.cnblogs.com/sure-feng/p/9740457.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!