python-configparser模块,xml.etree模块
操作键值对文件
#文件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模块的更多相关文章
- python标准库xml.etree.ElementTree的bug
使用python生成或者解析xml的方法用的最多的可能就数python标准库xml.etree.ElementTree和lxml了,在某些环境下使用xml.etree.ElementTree更方便一些 ...
- [python 学习] 使用 xml.etree.ElementTree 模块处理 XML
---恢复内容开始--- 导入数据(读文件和读字符串) 本地文件 country_data.xml <?xml version="1.0"?> <data> ...
- Python全栈之路----常用模块----xml处理模块
xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融行业的很多系统的 ...
- Python3使用xml.dom.minidom和xml.etree模块儿解析xml文件,封装函数
总结了一下使用Python对xml文件的解析,用到的模块儿如下: 分别从xml字符串和xml文件转换为xml对象,然后解析xml内容,查询指定信息字段. from xml.dom.minidom im ...
- json,pickle,shelve模块,xml处理模块
常用模块学习—序列化模块详解 什么叫序列化? 序列化是指把内存里的数据类型转变成字符串,以使其能存储到硬盘或通过网络传输到远程,因为硬盘或网络传输时只能接受bytes. 为什么要序列化? 你打游戏过程 ...
- [python 2.x] xml.etree.ElementTree module
XML 文件:xmlparse.xml <?xml version="1.0" encoding="UTF-8" standalone="no& ...
- python ConfigParser、shutil、subprocess、ElementTree模块简解
ConfigParser 模块 一.ConfigParser简介ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类 ...
- python基础——15(加密、excel操作、ini文件操作、xml操作模块及数据格式分类)
一.加密模块 1.有解密的加密方式(base64) #base64加密 import base64 str_encrypt = input("输入要加密的字符串:\n") base ...
- [python标准库]XML模块
1.什么是XML XML是可扩展标记语言(Extensible Markup Language)的缩写,其中的 标记(markup)是关键部分.您可以创建内容,然后使用限定标记标记它,从而使每个单词. ...
随机推荐
- IOS 添加定时器(NSTimer)
定时器 CADisplayLink:时间间隔比较小(使用时间频率高)的时候用(适合小游戏中开发) NSTimer:时间间隔比较大的时候调用(适合图片轮放的时候用) //声明定时器 @property ...
- 2017.9.25 JSP内置对象的概述
1.JSP的定义: 在JSP中是为了便于数据信息的存储.传递.获取,专门设置了九个内置对象, jsp内置对象是指他们是预先设定的,不需创建,每个对象都有自己的属性和方法. 2.JSP内置对象 对象名称 ...
- Entity Framework 连接 mysql 。(code first模式)
准备工作 1.下载vs2015 2.下载mysql2017 3.安装 1.创建类库 . 2.打开Nuget包,下载最新版的entity framewor. 3.在引用中添加 mysql.data; m ...
- OpenMP常用函数
1.设置线程数目 定义如下: void omp_set_num_threads(int num_threads); 通过该函数来指定其后用于并行计算的线程数目,其中参数num_threads就是指定的 ...
- angularJS 提示
1.通常angularJS应用程序将模块和控制器包含在JavaScript文件中. 2.在模块的定义中[]参数是用来表示模块的依赖关系. 3.中括号[]为空,表示该模块没有依赖关系,如果有依赖关系的话 ...
- base_lr, blobs_lr
caffe里面,原来以为是不可以随便调整学习率的,现在看来是可以的.base_lr是适用于所有层的学习率,而针对单个层,可以通过增加两个blobs_lr,用来调整该层的学习率,为什么是两个呢,因为一个 ...
- Python爬虫,看看我最近博客都写了啥,带你制作高逼格的数据聚合云图
转载请标明出处: http://blog.csdn.net/forezp/article/details/70198541 本文出自方志朋的博客 今天一时兴起,想用python爬爬自己的博客,通过数据 ...
- 一句话说明==和equals的区别
public class equals { public static void main(String[] args) { int x=10; int y=10; String str1=new S ...
- JavaScript: window.onload = function() {} 里面的函数不执行
问题:写了一个最简单的页面.在script标签中使用的 window.onload = function() { function add() { //... } } 页面上:<div oncl ...
- 文本处理工具-AWK
awk简介 awk功能与sed相似,都是用来进行文本处理的.awk可以自动地搜索输入文件,并把每一个输入行切分成字段.许多工作都是自动完成的,例如读取每个输入行.字段分割. awk工作原理 awk一次 ...