Python使用ConfigParser模块读取配置文件(config.ini)以及写入配置文件
前言
使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是configParser。configParser解析的配置文件的格式比较象ini的配置文件格式,就是文件中由多个section构成,每个section下又有多个配置项。括号“[ ]”内包含的为section。紧接着section 为类似于key-value 的options 的配置内容。
比如,我的目录如下,在test_config下有一个config.ini配置文件
一、ConfigParser简介
ConfigParser 是用来读取配置文件的包。配置文件的格式如下:中括号“[ ]”内包含的为section。section 下面为类似于key-value 的配置内容。括号“[ ]”内包含的为section。紧接着section 为类似于key-value 的options 的配置内容。
# 定义config分组
[config]
platformName=Android
appPackage=com.romwe
appActivity=com.romwe.SplashActivity # 定义cmd分组
[cmd]
viewPhone=adb devices
startServer=adb start-server
stopServer=adb kill-server
install=adb install aaa.apk
id=1
weight=12.1
isChoice=True # 定义log分组
[log]
log_error=true
二、ConfigParser 初始化对象
使用ConfigParser 首选需要初始化实例,并读取配置文件:
PS:python3里面自带configparser模块来读取ini文件,敲黑板:python2的版本是Configparser
# python3
import configParser
config = configparser.ConfigParser()
config.read("config_ini", encoding="utf-8")
三、ConfigParser 常用方法
1、获取所用的section节点
# 获取所用的section节点
import configparser
config = configparser.ConfigParser()
config.read("config_ini", encoding="utf-8")
print(config.sections())
#运行结果
# ['config', 'cmd', 'log']
2、获取指定section 的options。即将配置文件某个section 内key 读取到列表中:
import configparser
config = configparser.ConfigParser()
config.read("config_ini", encoding="utf-8")
r = config.options("config")
print(r)
#运行结果
# ['platformName', 'appPackage', 'appActivity']
3、获取指点section下指点option的值
import configparser
config = configparser.ConfigParser()
config.read("config.ini", encoding="utf-8")
r = config.get("config", "platformName")
# r1 = config.getint("db", "k1") #将获取到值转换为int型
# r2 = config.getboolean("db", "k2" ) #将获取到值转换为bool型
# r3 = config.getfloat("db", "k3" ) #将获取到值转换为浮点型
print(r)
#运行结果 # Android
4、获取指点section的所用配置信息
import configparser
config = configparser.ConfigParser()
config.read("config.ini", encoding="utf-8")
r = config.items("platformName")
print(r)
#运行结果
#[('platformName', 'Android'), ('appPackage', 'com.romwe'), ('appActivity', 'com.romwe.SplashActivity')]
5、修改某个option的值,如果不存在则会出创建
# 修改某个option的值,如果不存在该option 则会创建
import configparser
config = configparser.ConfigParser()
config.read("config_ini", encoding="utf-8")
config.set("config", "platformName", "ios") #修改platformName的值为ios
config.write(open("config.ini", "w"))
# 运行结果 # 定义config分组
[config]
platformName=ios
appPackage=com.romwe
appActivity=com.romwe.SplashActivity # 定义cmd分组
[cmd]
viewPhone=adb devices
startServer=adb start-server
stopServer=adb kill-server
install=adb install aaa.apk
id=1
weight=12.1
isChoice=True # 定义log分组
[log]
log_error=true
6、检查section或option是否存在,bool值
import configparser
config = configparser.ConfigParser()
config.has_section("config") #是否存在该section
config.has_option("config", "platformName") #是否存在该option
7、添加section 和 option
import configparser
config = configparser.ConfigParser()
config.read("config.ini", encoding="utf-8")
if not config.has_section("brand"): # 检查是否存在section
config.add_section("brand")
if not config.has_option("brand", "China"): # 检查是否存在该option
config.set("brand", "China", "xiaomi")
config.write(open("config.ini", "w"))
# 运行结果 # 定义config分组
[config]
platformName=Android
appPackage=com.romwe
appActivity=com.romwe.SplashActivity # 定义cmd分组
[cmd]
viewPhone=adb devices
startServer=adb start-server
stopServer=adb kill-server
install=adb install aaa.apk
id=1
weight=12.1
isChoice=True # 定义log分组
[log]
log_error=true [brand]
China=xiaomi
8、删除section 和 option
import configparser
config = configparser.ConfigParser()
config.read("config.ini", encoding="utf-8")
config.remove_section("brand") #整个section下的所有内容都将删除
config.write(open("config.ini", "w"))
# 定义config分组
[config]
platformName=Android
appPackage=com.romwe
appActivity=com.romwe.SplashActivity # 定义cmd分组
[cmd]
viewPhone=adb devices
startServer=adb start-server
stopServer=adb kill-server
install=adb install aaa.apk
id=1
weight=12.1
isChoice=True # 定义log分组
[log]
log_error=true
9、写入文件
以下的几行代码只是将文件内容读取到内存中,进过一系列操作之后必须写回文件,才能生效。
import configparser
config = configparser.ConfigParser()
config.read("comfig.ini", encoding="utf-8")
写回文件的方式如下:(使用configparser的write方法)
config.write(open("config.ini", "w"))
Python使用ConfigParser模块读取配置文件(config.ini)以及写入配置文件的更多相关文章
- python中configparser模块读取ini文件
python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...
- python-通过configparser模块读取后缀为 .ini 的配置文件信息
前言 一般为了方便会将路径,连接信息等写到配置文件(通常会将这些信息写到yaml,ini....配置文件)中,configparser模块读取后缀为 .ini 的配置文件信息 配置文件格式 #存在 c ...
- Python的ConfigParser模块读取ini配置文件 报错(持续更新总结)
1.ConfigParser.MissingSection什么的错误巴拉巴拉一堆,其实根本上就是没有读到配置文件,然后我去检查了一遍路径,发现没有问题,我是将文件的路径作为一个字符串拼接好传到另一个专 ...
- python封装configparser模块获取conf.ini值
configparser模块是python自带的从文件中获取固定格式参数的模块,因为是python只带的,大家用的应该很多,我觉得这个参数模块比较灵活,添加参数.修改参数.读取参数等都有对应的参数供用 ...
- python封装configparser模块获取conf.ini值(优化版)
昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...
- python中confIgparser模块学习
python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
- python 的ConfigParser模块
Python 之ConfigParser模块 一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.sect ...
- python中configparser模块
python中的configparse模块的使用 主要用来解析一些常用的配置,比如数据配置等. 例如:有一个dbconfig.ini的文件 [section_db1] db = test_db1 ho ...
- Python中ConfigParser模块应用
Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser.ConfigParser和SafeConf ...
随机推荐
- ~/.emacs emacs 配置文件
windows ~/.emacs (when (>= emacs-major-version 24) (require 'package) (add-to-list 'package-archi ...
- Mac里存储空间不足,该怎么删垃圾数据?
说明:在mac设备运行一段时间后,电脑空间很小了,对于开发者来说,清清Xcode缓存,腾出几十G的空间还是有可能的.在升级Xcode适配新系统.新手机也是得给电脑减减压. 一.Xcode缓存文件(co ...
- CentOS7安装Python3和VIM8
参考:http://blog.sina.com.cn/s/blog_45249ad30102yulz.html
- The type name or alias SqlServer could not be resolved.Please check your configuration
The type name or alias SqlServer could not be resolved.Please check your configuration file.... 检查一下 ...
- hexo访问优化之--------gulp压缩
hexo访问优化之--------gulp压缩 hexo生成的博客是静态html页面,当有很多静态资源时,加载速度会非常慢,且github服务器在国外,导致网页加载速度非常差 gulp压缩 gulp是 ...
- 题解 CF961G 【Partitions】
题目传送门 题目大意 给出\(n,k\),以及\(w_{1,2,..,n}\),定义一个集合\(S\)的权值\(W(S)=|S|\sum_{x\in S} w_x\),定义一个划分\(R\)的权值为\ ...
- 洛谷4631 [APIO2018] Circle selection 选圆圈 (KD树)
qwq纪念AC450 一开始想这个题想复杂了. 首先,正解的做法是比较麻烦的. qwqq 那么就不如来一点暴力的东西,看到平面上点的距离的题,不难想到\(KD-Tree\) 我们用类似平面最近点对那个 ...
- uoj21 缩进优化(整除分块,乱搞)
题目大意: 给定一个长度为\(n\)的序列 让你找一个\(x\),使得\(ans\)尽可能小 其中$$ans=\sum_{i=1}^{n}\lfloor\frac{a_i}{x}\rfloor + \ ...
- 电脑(windows)端口被占用如何解决
问题: 今天在启动项目的时候,控制台提示"8080端口被占用",此时我并没有启动其他项目.那么8080端口被占用解决方法如下: 1.点击左下角"开始",在搜索框 ...
- 永久修改alias
永久修改alias home目录下ls -a显示隐藏文件 编辑./cshrc