python教程:ConfigParser模块使用教程
1.简介
我们经常需要使用配置文件,例如.conf和.ini等类型,使用ConfigPaser模块可以对配置文件进行操作。
2.示例
现有配置文件test.ini,其内容如下:
[section_a]
a_key1 = str content
a_key2 = 10[section_b]
b_key1 = b_value1
b_key2 = b_value2
1读取配置文件
import ConfigParser
import os# 生成config对象
os.chdir('C:\\Study\\python\\configparser')
cf = ConfigParser.ConfigParser()
# 读取配置文件
cf.read("test.ini")
2读取数据
# 读取所有节
sections = cf.sections()
print 'sections:', sections
结果如下:
# 读取指定节的键
opts = cf.options('section_a')
print('options:', opts)
结果如下:
# 读取指定节的所有键值对
kvs = cf.items('section_a')
print ('section_a:', kvs)
结果如下:
# 读取指定节和键的值
# 主要使用的有get()、getint()方法,前者为str类型,后者为int类型
kv1 = cf.get('section_a', 'a_key1')
print kv1, type(kv1)
kv2 = cf.getint('section_a', 'a_key2')
print kv2, type(kv2)
结果如下:
3写入数据
更新指定节和键的值
cf.set('section_b', 'b_key1', 'new_value1')
结果如下:
[section_a]
a_key1 = str content
a_key2 = 10[section_b]
b_key1 = new_value1
b_key2 = b_value2
对指定节,新增键
cf.set('section_b', 'b_key3')
结果如下:
[section_a]
a_key1 = str content
a_key2 = 10[section_b]
b_key1 = new_value1
b_key2 = b_value2
b_key3 = None
对指定节,新增键值对
cf.set("section_b", "b_new_key", "b_new_value")
结果如下:
'''
学习中遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
[section_a]
a_key1 = str content
a_key2 = 10[section_b]
b_key1 = new_value1
b_key2 = b_value2
b_key3 = None
b_new_key = b_new_value
新增节
cf.add_section('section_c')
结果如下:
[section_a]
a_key1 = str content
a_key2 = 10[section_b]
b_key1 = new_value1
b_key2 = b_value2
b_key3 = None
b_new_key = b_new_value[section_c]
在所有写入完毕后,进行保存操作:
# 写入文件
cf.write(open('test.ini', 'w'))
python教程:ConfigParser模块使用教程相关推荐
- python中confIgparser模块学习
python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
- 【Python的configparser模块读取.ini文件内容并输出】
configparser是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近.Python2.x 中名为 ConfigParser,3.x 已更名小写.下文通过使用python ...
- Python之Bs4模块安装教程
Python安装Bs4模块的方法有多种,我这里主要演示自己实际安装成功的方法. 1.在终端打开powershell,输入执行指令(前提是需要支持pip指令): pip3 install Beautif ...
- Python之configparser模块详解和使用
1 configparser安装 pip3 install configparser 2 configparser简介 用来读取配置文件的python包: 一般做自动化测试的时候,会使用到这个模块,用 ...
- ConfigParser模块简明教程
参考官方文档: https://docs.python.org/3.4/library/configparser.html Supported INI File Structure Please no ...
- 飘逸的python - 简明gzip模块压缩教程
压缩数据创建gzip文件 先看一个略麻烦的做法 import StringIO,gzip content = 'Life is short.I use python' zbuf = StringIO. ...
- Python基础-ConfigParser模块
此模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 来看一个好多软件的常见配置文件格式如下 [DEFAULT] ServerAliveI ...
- python 之configparser模块
该模块的作用 就是使用模块中的RawConfigParser().ConfigParser(). SafeConfigParser()这三个方法(三者择其一),创建一个对象使用对象的方法对指定的配置文 ...
- python中configparser_python中confIgparser模块学习
python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
最新文章
- Python裁剪图片(Crop an Image)
- 接受者操作特征曲线ROC
- Jackson 序列化对象成 JSON 字符串,忽略部分字段(属性)
- No such file or directory: Failed to enable the 'httpready' Accept Filter
- 学习笔记之四_Cisco系统IOS和安全设备管理系统SDM(CCNA知识考点)
- android studio 导入第三方库的记录
- 【原创】linux 批量清空文本内容
- 阅读宋立恒《AI制胜:机器学习极简入门》第2章:机器学习中的数据预处理
- java中int边界值_数组中重复的数字2019.12.06
- Wampserver查看php配置信息
- signature=fd45b8c9a90eebce5d855f07302ab4ee,Private Use Area
- Python常用基础语法
- win7 插入鼠标自动禁用触摸板
- python | 降水数据分析(Ⅰ) 绘制全国降水四季分布图
- 区块链报告会心得体会3000_实习心得体会3000字篇【优秀篇】
- Kubernetes全套笔记
- 公式图片转latex神器Mathpix以及latex公式与word公式的相互转换
- Python 串级PID代码
- HTTP Status 406 -with characteristics not acceptable according to the request accept headers.
- 用批处理自动设置IE代理