day22 Pythonpython 本文xml模块

一、xml介绍

 xml是实现不同语言或者程序直接进行数据交换的协议,跟json差不多,单json使用起来更简单。不过现在还有很多传统公司的接口主要是xml

xml跟html都是标签语言

我们主要学习的是ElementTree。是Python的xml处理模块,他提供了一个轻量级的对象模型,在使用ElementTree模块时,需要import xml.etree.ElementTre

ElementTree相当于整个xml的节点数,而Element表示节点树中的一个单独节点

我们看下面的xml文本,标签分为两种。

1、自闭和标签<rank updated="yes">2</rank>

2、非自闭和标签<neighbor direction="E" name="Austria" />

xml文件

<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year updated="yes">2010</year>
        <gdppc>141100</gdppc>
        <neighbor direction="E" name="Austria" />
        <neighbor direction="W" name="Switzerland" />
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year updated="yes">2013</year>
        <gdppc>59900</gdppc>
        <neighbor direction="N" name="Malaysia" />
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year updated="yes">2013</year>
        <gdppc>13600</gdppc>
        <neighbor direction="W" name="Costa Rica" />
        <neighbor direction="E" name="Colombia" />
    </country>
</data>

 二、xml.etree.ElementTree模块的具体使用

1、打印跟标签的名字

import xml.etree.ElementTree as ET #导入模块,名字太长了,把这个模块名重命名为ET
tree = ET.parse("xml_lesson.xml")#parse解析,用ET模块下的parse这个方法把xml文件解析开,解析开拿到一个tree,tree就是一个对象
root = tree.getroot()#这个对象可以调用方法,getroot就是根的意思
print(root.tag)#root这个对象有一个属性tag,tag的值就是根标签的名字

结果:
data

什么是标签,什么是属性,举个例子

<country name="Liechtenstein">

conuntry是标签 

name="Liechtenstein" 是这个标签的属性

<neighbor direction="W" name="Costa Rica" />

neighbor是标签

direction="W" name="Costa Rica"  这2个都是标签的属性

2、用for循环查看root下面试什么东西

for n in root:
    print(n)

结果:
<Element 'country' at 0x0000000000E339F8>

<Element 'country' at 0x0000000000E38408>

<Element 'country' at 0x0000000000E38598>

 这三个地址指的就是面向对象,打印下这三个标签的名字,用tag这个属性就可以了

for n in root:
    print(n.tag)

结果:
country

country

country

 在打印下country下面的标签

for n in root:
    for i in n:
        print(i.tag)

结果:
rank

year

gdppc

neighbor

neighbor

rank

year

gdppc

neighbor

rank

year

gdppc

neighbor

neighbor

 3、打印标签属性attrib

打印country的属性

for n in root:
    print(n.attrib)

结果:
{'name': 'Liechtenstein'}

{'name': 'Singapore'}

{'name': 'Panama'}

 在打印country里面的对象的属性

for n in root:
    for i in n:
        print(i.attrib)

结果:
{'updated': 'yes'}

{'updated': 'yes'}

{}

{'name': 'Austria', 'direction': 'E'}

{'name': 'Switzerland', 'direction': 'W'}

{'updated': 'yes'}

{'updated': 'yes'}

{}

{'name': 'Malaysia', 'direction': 'N'}

{'updated': 'yes'}

{'updated': 'yes'}

{}

{'name': 'Costa Rica', 'direction': 'W'}

{'name': 'Colombia', 'direction': 'E'}

 4、text标签实际包裹的内容

for n in root:
    for i in n:
        print(i.text)



结果:
data
2
2010
141100
None
None
5
2013
59900
None
69
2013
13600
None
None

 5、iter

for n in root.iter("year"):
    print(n.tag,n.text)

结果:
data
year 2010
year 2013
year 2013

 6、对xml文件数据进行修改操作

import xml.etree.ElementTree as ET #导入模块,名字太长了,把这个模块名重命名为ET

tree = ET.parse("xml_lesson.xml")#parse解析,用ET模块下的parse这个方法把xml文件解析开,
                                   #解析开拿到一个tree,tree就是一个对象
root = tree.getroot()#这个对象可以调用方法,getroot就是根的意思
# print(root.tag)#root这个对象有一个属性tag,tag的值就是根标签的名字

for n in root.iter("year"):
    new_year=int(n.text)+1
    n.text=str(new_year)#修改year标签的text属性
    n.set("updated1","yes")#给year这个标签增加一个属性
tree.write("xml_lesson.xml")#直接把修改的写入到文件中

猜你喜欢

转载自www.cnblogs.com/charon2/p/10405719.html