操作键值对文件

 #文件db格式为
[section]
a = 1
b = 2 [section1]
d = 3
c = 4 import configparser #获取所有节点
config = configparser.ConfigParser()
config.read('db')
ret = config.sections()
print(ret) >>>['section', 'section1'] #获取指定节点下所有的键值对
ret = config.items('section1')
print('获取键值对:',ret)
>>>获取键值对: [('d', ''), ('c', '')] #获取所有的键
ret = config.options('section')
print('获取节点下键名称:',ret)
>>>获取节点下键名称: ['a', 'b'] #获取节点下的指定key的值
ret =config.get('section1','c')
print(ret)
>>>4 #检查节点是否存在
has_sec = config.has_section('section1')
print(has_sec)
>>>True #添加节点section2
config.add_section('section2')
config.write(open('db','w')) #db文件当前为
[section]
a = 1
b = 2 [section1]
d = 3
c = 4 [section2] #删除节点section2
config.remove_section('section2')
config.write(open('db','w')) #当前db文件内容为
[section]
a = 1
b = 2 [section1]
d = 3
c = 4 #检查删除设置指定组内的键值对是否存在
has_opt = config.has_option('section1','c')
print(has_opt)
>>>True #删除
config.remove_option('section1','c')
config.write(open('db','w'))
#当前db文件内容为
[section]
a = 1
b = 2 [section1]
d = 3 #设置更改指定值
config.set('section1','d','')
config.write(open('db','w'))
#db内容
[section]
a = 1
b = 2 [section1]
d = 123
c = 4
 #oo.xml文件内容
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year>2023</year>
<year>2027</year>
<year>2027</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
</country>
</data> from xml.etree import ElementTree as ET #打开xml文件加载到内存
tree = ET.parse('oo.xml')
#获取根节点
root = tree.getroot() for child in root:
print(child.tag,child.attrib)#child.tag,子节点标签名,子节点属性
for gradechild in child:
print(gradechild.tag,gradechild.text,gradechild.attrib) >>>country {'name': 'Liechtenstein'}
>>>rank 2 {'updated': 'yes'}
>>>year 2023 {}
>>>gdppc 141100 {}
>>>neighbor None {'direction': 'E', 'name': 'Austria'}
>>>neighbor None {'direction': 'W', 'name': 'Switzerland'} #两种方法读取xml文件直接解析,tree对象是ElementTree对象
tree = ET.parse('oo.xml')
root = tree.getroot() #将xml已字符串格式获得,将字符串转化为element对象,无法进行写操作
str_xml = open('oo.xml','r').read()
root = ET.XML(str_xml) #循环所有的year节点
for node in root.iter('year'):
#将year节点中的内容自增一
new_year = int (node.text) +1
node.text = str(new_year)
print(node.text)
>>>2024
>>>2027
>>>2027
#设置属性
node.set('name','alex')
node.set('age','')
print(node.attrib)
>>>{'age': '', 'name': 'alex'}
>>>{'age': '', 'name': 'alex'}
>>>{'age': '', 'name': 'alex'}
#删除属性
del node.attrib['name']
print(node.attrib)
>>>{'age': ''}
>>>{'age': ''}
>>>{'age': ''}
#保存文件
tree = ET.ElementTree(root)
tree.write("newnew.xml", encoding='utf-8')
 #创建根节点
root = ET.Element('family') #创建大儿子
son1 = ET.Element('son',{'name':'儿子1'})
#创建二儿子
son2 = ET.Element('son',{'name':'儿子2'}) #大儿子中创建两个孙子
grandson1 = ET.Element('grandson',{'name':'孙子1'})
grandson2 = ET.Element('grandson',{'name':'孙子2'})
son1.append(grandson1)
son2.append(grandson2) root.append(son1)
root.append(son2) tree = ET.ElementTree(root)
tree.write('ooo.xml',encoding = 'utf-8',short_empty_elements = False)
#ooo.xml文件为
<family><son name="儿子1"><grandson name="孙子1"></grandson></son><son name="儿子2"><grandson name="孙子2"></grandson></son></family>

python-configparser模块,xml.etree模块的更多相关文章

  1. python标准库xml.etree.ElementTree的bug

    使用python生成或者解析xml的方法用的最多的可能就数python标准库xml.etree.ElementTree和lxml了,在某些环境下使用xml.etree.ElementTree更方便一些 ...

  2. [python 学习] 使用 xml.etree.ElementTree 模块处理 XML

    ---恢复内容开始--- 导入数据(读文件和读字符串) 本地文件 country_data.xml <?xml version="1.0"?> <data> ...

  3. Python全栈之路----常用模块----xml处理模块

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

  4. Python3使用xml.dom.minidom和xml.etree模块儿解析xml文件,封装函数

    总结了一下使用Python对xml文件的解析,用到的模块儿如下: 分别从xml字符串和xml文件转换为xml对象,然后解析xml内容,查询指定信息字段. from xml.dom.minidom im ...

  5. json,pickle,shelve模块,xml处理模块

    常用模块学习—序列化模块详解 什么叫序列化? 序列化是指把内存里的数据类型转变成字符串,以使其能存储到硬盘或通过网络传输到远程,因为硬盘或网络传输时只能接受bytes. 为什么要序列化? 你打游戏过程 ...

  6. [python 2.x] xml.etree.ElementTree module

    XML 文件:xmlparse.xml <?xml version="1.0" encoding="UTF-8" standalone="no& ...

  7. python ConfigParser、shutil、subprocess、ElementTree模块简解

    ConfigParser 模块 一.ConfigParser简介ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类 ...

  8. python基础——15(加密、excel操作、ini文件操作、xml操作模块及数据格式分类)

    一.加密模块 1.有解密的加密方式(base64) #base64加密 import base64 str_encrypt = input("输入要加密的字符串:\n") base ...

  9. [python标准库]XML模块

    1.什么是XML XML是可扩展标记语言(Extensible Markup Language)的缩写,其中的 标记(markup)是关键部分.您可以创建内容,然后使用限定标记标记它,从而使每个单词. ...

随机推荐

  1. SQL的注入式攻击方式和避免方法

    SQL 注入是一种攻击方式,在这种攻击方式中,恶意代码被插入到字符串中,然后将该字符串传递到 SQL Server 的实例以进行分析和执行.任何构成 SQL 语句的过程都应进行注入漏洞检查,因为 SQ ...

  2. eplise中运行提示 A fatal error has been detected by the java runtime environment

    今天一同事出现运行项目时,提示 A fatal error has been detected by the java runtime environment,具体表现是使用我们框架,不能正常的打印日 ...

  3. centos7安装python3和ipython

    CentOS7下默认系统自带python2.X的版本,这个版本被系统很多程序所依赖,所以不建议删除,如果使用最新的Python3那么我们知道编译安装源码包和系统默认包之间是没有任何影响的,所以可以安装 ...

  4. css中hover设置边框后div中内容后移解决方法

    <style> .demo{width:1200px;height:400px;background:#fff;} .demo:hover{border:1px solid #cecece ...

  5. ES6介绍

    1.ES6简介 ECMAScript 6.0,是 JavaScript 语言下一代标准,发布于 2015 年 6 月.它的目标是使得 JavaScript 语言可以用来编写复杂的大型应用程序,成为企业 ...

  6. 基于SOA架构和流媒体技术的在线教育平台的研究

    简介 现代远程教育是指通过音频.视频(直播或录像)以及包括实时和非实时在内的计算机技术把课程传送的教育.现代远程教育是随着现代信息技术的发展而产生的一种新型教育方式.计算机技术.多媒体技术.通信技术的 ...

  7. vue2.x结合百度UEditor富文本编辑器

    1.首先下载UEditor源码(https://ueditor.baidu.com/website/),将整个文件放到static文件夹中 2.在src/components文件夹下创建公共组件UEd ...

  8. Map the Debris -freecodecamp算法题目

    Map the Debris 1.要求 返回一个数组,其内容是把原数组中对应元素的平均海拔转换成其对应的轨道周期. 原数组中会包含格式化的对象内容,像这样 {name: 'name', avgAlt: ...

  9. 原生Ajax发送请求

    ajax  get&post 1.使用get发送请求,会有请求缓存 1)什么叫请求缓存,请求信息相同浏览器不会再向服务器发送请求,导致访问服务器失败. 2)解决:将随机数添加到请求路径后面参数 ...

  10. 描述linux目录结构以及目录结构命名规定

    FHS全称(Filesystem Hierarchy Standard),中文意思是目录层次标准,是linux的目录规范标准. 详情点击查看 FHS定义了两层规范: 第一层:“/”目录下的各个目录应该 ...