zoukankan      html  css  js  c++  java
  • xml模块

     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 '''
  • 相关阅读:
    数据库主体拥有数据库角色,无法删除
    popupWin类QQ弹出窗口
    使用性能监视器找出硬件瓶颈
    关于反射中Assembly.CreateInstance()与Activator.CreateInstance()方法的区别
    大数据的通用分页总结!
    SQL 注入[转自:微软技术中心]
    【转】 使用Yahoo的公开API做天气预报
    JS对select动态添加options操作
    asp.net “从客户端检测到有潜在危险的Request.Form值” 处理办法
    SQL中索引的原理
  • 原文地址:https://www.cnblogs.com/sure-feng/p/9740457.html
Copyright © 2011-2022 走看看