加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml模块

发布时间:2020-12-16 23:44:11 所属栏目:百科 来源:网络整理
导读: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 year2008/year 10 gdppc141100/gdppc 11 neighbor name="Austria" direction="E"/ 12 neighbor name="Switzerland
 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,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 ‘‘‘

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读