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)是关键部分.您可以创建内容,然后使用限定标记标记它,从而使每个单词. ...
随机推荐
- POJ-1159 Palindrome---变成回文串的最小代价
题目链接: https://cn.vjudge.net/problem/POJ-1159 题目大意: 题意很明确,给你一个字符串,可在任意位置添加字符,最少再添加几个字符,可以使这个字符串成为回文字符 ...
- POJ-1840 Eqs---二分
题目链接: https://vjudge.net/problem/POJ-1840 题目大意: 给出一个5元3次方程,输入其5个系数,求它的解的个数 其中系数 ai∈[-50,50] 自变量xi∈[ ...
- POJ-3669 Meteor Shower---BFS+预处理
题目链接: https://vjudge.net/problem/POJ-3669 题目大意: 巨大流星雨即将袭来.每个流星会对击中的地方以及周围(上下左右四格)造成破坏.Bessie开始时位于(0, ...
- 谷歌Web中文开发手冊:3响应式
https://developers.google.com/web/fundamentals/getting-started/your-first-multi-screen-site/responsi ...
- OpenCV2马拉松第5圈——线性滤波
收入囊中 这里的非常多内容事实上在我的Computer Vision: Algorithms and ApplicationsのImage processing中都有讲过 相关和卷积工作原理 边界处理 ...
- LA 3635 派
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- centos下yum安装mysql5.6后,无法启动 MySQL Daemon failed to start
如果是全新安装应该就不会出现这个问题,升级安装的话,要运行 mysql_upgrade ,但是启动MYSQL就报错MySQL Daemon failed to start 如此就没办法运行mysql_ ...
- EF 连接 mysq l数据库 code first模式 的实践
准备工作: 1.下载vs2015 2.下载mysql2017 3.安装 开始: 1.创建 控制台文件 2.添加引用 Mysql.Data , Mysql.Data.Entity.EF6,Mysql.w ...
- Java的感受
感觉Java很重要,但是学起来好像并不比C语言简单.
- bootstrap suggest搜索建议插件
近日因工作需要看了下此插件. 首先下载bootstrap js包.添加此插件的引用.注意css样式要引用,不能忘记. 前台页面代码,因为楼主做的是选项卡切换查询不同的结果. <tr> &l ...