Python3 从零单排10_xml&configparser

  xml模块:

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

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

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

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

import xml.etree.ElementTree as ET

tree = ET.parse("test.xml") #类似文件的打开open
root = tree.getroot() #f.seek(0)
print(dir(root))  #查看根的方法 append,clear,extend,find,findall,findtext,get,getchildren,getiterator,insert,items,iter,iterfind,itertext,keys,makeelement,remove,set
print(root.tag)  #拿到根标签

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

# 只遍历year节点
for node in root.iter("year"):
    print(node.tag,node.attrib,node.text)

#增删改查
#把每年+1
for node in root.iter("year"):
    new_year = int(node.text)+1
    node.text = str(new_year)
    node.set("attr_test2","yes")
tree.write("test.xml")

#删除node
for country in tree.findall("country"):
    rank = int(country.find("rank").text)
    if rank > 50:
        root.remove(country)
tree.write("out_put.xml")

#创建xml文档
root = ET.Element("namelist")
name = ET.SubElement(root,"name",attrib={"enrolled":"yes"})
age = ET.SubElement(name,"age",attrib={"checked":"no"})
sex = ET.SubElement(name,"sex")
sex.text="female"

name2 = ET.SubElement(root,"name",attrib={"enrolled":"no"})
age2 = ET.SubElement(name2,"age",attrib={"checked":"no"})
age2.text = "18"

et = ET.ElementTree(root)
et.write("test2.xml",encoding = "utf-8",xml_declaration=True)  #xml_declaration=True xml版本说明

ET.dump(root)

  configparser模块

  本来字典够用,但很多如那件都会以config这种形式存储配置文件,所以python里内置了这个模块,本质和字典一样,嵌套字典

  config.ini配置文件如下

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes

[bitbucket.org]
User = hg

[topsecret.server.com]
Port = 50022
ForwardX11 = no

  操作:

import configparser


conf = configparser.ConfigParser()  #实例化一个config文件解析的对象
conf.read("conf.ini")  #读取配置文件为一个字典对象
print(dir(conf))  #可以看到很多和字典类似
print(dir(conf["bitbucket.org"])) #可以看到很多和字典类似

#
print(conf.sections())  #不会打印default
print(conf.default_section)
print(conf["bitbucket.org"]["User"]) #获取配置项的值
for k in conf["bitbucket.org"]: #打印包含default的所有key,default默认所有节点都有该项配置,所以在循环其中一个节点,会打印default
    print(k)
for k,v in conf["bitbucket.org"].items():  #打印包含default的所有key/value
    print(k,v)
print(conf.has_section("asd"))  #判断是否存在该节点
print(conf.has_section("bitbucket.org"))
print(conf.has_option("bitbucket.org","asdfg"))  #判断该节点是否存在该key
print(conf.has_option("bitbucket.org","User"))

#增删改  任何一个操作都要重新写文件才会生效
conf.read("test.ini")
print(conf.options("group1"))
print(conf["group1"]["k1"])

conf["group2"]["k1"] = "54321"
conf.set("group1","k1","123456")
conf.write(open("test1.ini","w"))  #写入文件才会生效

conf.remove_section("group2")  #删除节点,不指定group2的话,会删除所有section
conf.remove_option("group1","k1")  #删除节点下的项,必须指定项,不然报错
conf.write(open("test.ini","w"))  #可以直接以之前的文件命名,内容会覆盖

猜你喜欢

转载自www.cnblogs.com/znyyy/p/10072731.html