二、常用模块

一、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>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

xml协议在各个语言里的都 是支持的,在python中可以用以下模块操作xml:

xml模块

1、首先导入xml模块

2、parse()解析

3、getroot()获取根对象

xml:方法如下:

查看标签名,标签属性,标签中间的文本内容

每个子标签都有标签名字,属性,文本内容分别对象下面的方法

tag,attrib,text

下面分别是获取标签,属性,和文本内容

import xml.etree.cElementTree as ET

tree =ET.parse("a.xml")
root=tree.getroot()
c=((i.tag,i.attrib,i.text) for child in root for i in child)
print([i for i in c])

结果:

[('rank', {'updated': 'yes'}, '2'),
 ('year', {}, '2008'),
 ('gdppc', {}, '141100'),
 ('neighbor', {'direction': 'E', 'name': 'Austria'}, None), 
('neighbor', {'direction': 'W', 'name': 'Switzerland'}, None),
 ('rank', {'updated': 'yes'}, '5'),
 ('year', {}, '2011'),
 ('gdppc', {}, '59900'),
 ('neighbor', {'direction': 'N', 'name': 'Malaysia'}, None), 
('rank', {'updated': 'yes'}, '69'),
 ('year', {}, '2011'),
 ('gdppc', {}, '13600'), 
('neighbor', {'direction': 'W', 'name': 'Costa Rica'}, None), ('neighbor', {'direction': 'E', 'name': 'Colombia'}, None)]

查找elment元素的三种方式:

分别为iter  find  和findall,实例如下:

import xml.etree.cElementTree as ET

tree =ET.parse("a.txt")
root=tree.getroot()
c=((i.tag,i.attrib,i.text) for child in root for i in child)

# #查找element中的元素  方式一
years=root.iter("year") #扫描整个xml文档树,找到所有
while True:
    try:
        print(next(years))
    except StopIteration:
        break

#谁来调用,就从水的下一层开始找,只找一个
res1=root.find("country")
print(res1)
#谁来调用,就从谁的下一层开始找,找所有
res2=root.findall("country")
print(res2)

下面分别是xml的增删改查操作

修改标签元素

import xml.etree.cElementTree as ET

tree =ET.parse("a.xml")
root=tree.getroot()
c=((i.tag,i.attrib,i.text) for child in root for i in child)
years=root.iter("year")
for year in years:
    year.text=str(int(year.text)+1)   #获取文本内容
    year.set("update","yes")          #设置属性
    year.set("version","1.0")         #设置属性
tree.write("aa.xml")                  #把设置的属性写入文件

  

删除节点

注意下面的三元表达式必须要有else,否则会出错
for county in root.iter("country"):
    print(county)
    rank=county.find("rank")
    county.remove(rank) if int(rank.text)>10 else None

tree.write("aa.xml")

  

增加节点

#增加节点
for counry in root.iter("country"):
    e=ET.Element("pyrene")
    e.text="hello"
    e.attrib={"age":"20"}
    counry.append(e)
tree.write("aa.xml")

 

configparser模块

如下配置文件a.ini

[pyrene1]
yes = yes

[pyrene]
name = pyrene

要操作这个配置文件,首先要导入模块

import configparser

config=configparser.ConfigParser()
config.read("a.ini")

取值操作

#取值操作
print(config.sections())#查看标题
print(config.options(config.sections()[0])) #获取标题下标为0下的配置项
res=config.get("pyrene","name") #获取某个标题下的某个配置项的值
print("%s,%s"%(type(res),res))

res1=config.getint("pyrene","age")
print(type(res1))

res2=config.getboolean("pyrene","is_admin")
print(type(res2))  

删除操作

#修改操作  删除
config.remove_section("pyrene")
config.remove_option("pyrene1","name")

#必须要加下面操作才能对源文件操作
config.write(open("a.ini","w"))

增加操作

config.add_section("pyrene")
config.set("pyrene","name","pyrene")

config.write(open("a.ini","w"))

  

原文地址:https://www.cnblogs.com/pyrene/p/7484652.html