Python常用模块-xml

Python常用模块-xml

http://www.cnblogs.com/alex3714/articles/5161349.html

xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融行业的很多系统的接口还主要是xml。

xml的格式如下,就是通过<>节点来区别数据结构的:

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year>2015</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

打印根节点名称

#!/usr/bin/env python

import xml.etree.ElementTree as ET

tree = ET.parse("xmltest.xml")
root = tree.getroot()
print(root.tag)
#####
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/admin/PycharmProjects/s18/day5/模块/a_xml.py
data

遍历xml文档

import xml.etree.ElementTree as ET

tree = ET.parse("xmltest.xml")
root = tree.getroot()
print(root.tag)

# 遍历xml文档
for child in root:
    print('--------------------')
    print(child.tag,child.attrib)
    for i in child:
        print(i.tag,i.text)

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/admin/PycharmProjects/s18/day5/模块/a_xml.py

data

--------------------

country {'name': 'Liechtenstein'}

rank 2

year 2008

gdppc 141100

neighbor None

neighbor None

--------------------

country {'name': 'Singapore'}

rank 5

year 2011

gdppc 59900

neighbor None

--------------------

country {'name': 'Panama'}

rank 69

year 2015

gdppc 13600

neighbor None

neighbor None

 

进程已结束,退出代码0

 

遍历一个节点

#!/usr/bin/env python

import xml.etree.ElementTree as ET

tree = ET.parse("xmltest.xml")
root = tree.getroot()
print(root.tag)

for i in root.iter('year'):
    print(i.tag,i.text)

for i in root.iter('gdppc'):
    print(i.tag, i.text)

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/admin/PycharmProjects/s18/day5/模块/a_xml.py

data

year 2008

year 2011

year 2015

gdppc 141100

gdppc 59900

gdppc 13600

修改、增加

import xml.etree.ElementTree as ET

tree = ET.parse("xmltest.xml")
root = tree.getroot()
print(root.tag)
for node in root.iter('year'):
    new_year =  int(node.text) + 1
    node.text = str(new_year)
    node.set("updated", "yes")
    #node.set("test_K","test_VVV")

tree.write('xmltest3.xml')


<data>
&#160;&#160;&#160;&#160;<country name="Liechtenstein">
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<rank updated="yes">2</rank>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<year updated="yes">2009</year>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<gdppc>141100</gdppc>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<neighbor direction="E" name="Austria" />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<neighbor direction="W" name="Switzerland" />
&#160;&#160;&#160;&#160;</country>
&#160;&#160;&#160;&#160;<country name="Singapore">
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<rank updated="yes">5</rank>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<year updated="yes">2012</year>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<gdppc>59900</gdppc>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<neighbor direction="N" name="Malaysia" />
&#160;&#160;&#160;&#160;</country>
&#160;&#160;&#160;&#160;<country name="Panama">
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<rank updated="yes">69</rank>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<year updated="yes">2016</year>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<gdppc>13600</gdppc>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<neighbor direction="W" name="Costa Rica" />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<neighbor direction="E" name="Colombia" />
&#160;&#160;&#160;&#160;</country>
</data>

创建xml文件

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import  xml.etree.ElementTree as ET

new_xml = ET.Element("namelist")
name = ET.SubElement(new_xml, "张三", attrib={"是否登记": ""})
age = ET.SubElement(name, "年龄", attrib={"是否允许": ""})
age.text = '18'
sex = ET.SubElement(name, "性别")
sex.text = ''

name = ET.SubElement(new_xml, "李四", attrib={"是否登记": "no"})
age = ET.SubElement(name, "年龄")
age.text = '19'
sex = ET.SubElement(name,'性别')
sex.text = ''

et = ET.ElementTree(new_xml)  # 生成文档对象
et.write("test.xml", encoding="utf-8", xml_declaration=True)

ET.dump(new_xml)  # 打印生成的格式

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/admin/PycharmProjects/s18/day5/模块/a_xml.py
<namelist><张三 是否登记="是"><年龄 是否允许="是">18</年龄><性别>男</性别></张三><李四 是否登记="no"><年龄>19</年龄><性别>女</性别></李四></namelist>

进程已结束,退出代码0


调整一下

<?xml version='1.0' encoding='utf-8'?>
<namelist>
    <张三 是否登记="是">
        <年龄 是否允许="是">18</年龄>
        <性别>男</性别>
    </张三>
    <李四 是否登记="no">
        <年龄>19</年龄>
        <性别>女</性别>
    </李四>
</namelist>

 

posted on 2018-07-06 11:31  光阴8023  阅读(119)  评论(0编辑  收藏  举报