python 的ConfigParser模块
Python 之ConfigParser模块
一、ConfigParser简介
ConfigParser 是用来读取配置文件的包。配置文件的格式如下:中括号“[ ]”内包含的为section。section 下面为类似于key-value 的配置内容。
[db]
db_host = 127.0.0.1
db_port = 69
db_user = root
db_pass = root
host_port = 9205 [concurrent]
thread =
processor =
括号“[ ]”内包含的为section。紧接着section 为类似于key-value 的options 的配置内容。
二、ConfigParser 初始化对象
使用ConfigParser 首选需要初始化实例,并读取配置文件:
import configparser
config = configparser.ConfigParser()
config.read("ini", encoding="utf-8")
三、ConfigParser 常用方法
1、获取所用的section节点
# 获取所用的section节点
import configparser
config = configparser.ConfigParser()
config.read("ini", encoding="utf-8")
print(config.sections())
#运行结果
# ['db', 'concurrent']
2、获取指定section 的options。即将配置文件某个section 内key 读取到列表中:
import configparser
config = configparser.ConfigParser()
config.read("ini", encoding="utf-8")
r = config.options("db")
print(r)
#运行结果
# ['db_host', 'db_port', 'db_user', 'db_pass', 'host_port']
3、获取指点section下指点option的值
import configparser
config = configparser.ConfigParser()
config.read("ini", encoding="utf-8")
r = config.get("db", "db_host")
# r1 = config.getint("db", "k1") #将获取到值转换为int型
# r2 = config.getboolean("db", "k2" ) #将获取到值转换为bool型
# r3 = config.getfloat("db", "k3" ) #将获取到值转换为浮点型
print(r)
#运行结果
# 127.0.0.1
4、获取指点section的所用配置信息
import configparser
config = configparser.ConfigParser()
config.read("ini", encoding="utf-8")
r = config.items("db")
print(r)
#运行结果
#[('db_host', '127.0.0.1'), ('db_port', '69'), ('db_user', 'root'), ('db_pass', 'root'), ('host_port', '69')]
5、修改某个option的值,如果不存在则会出创建
>>> import configparser
>>> config = configparser.ConfigParser()
>>> config.read("ini", encoding="utf-8")
['ini']
>>> r = config.options("db")
>>> print(r)
['db_host', 'db_port', 'db_user', 'db_pass', 'host_port']
>>> # 修改某个option的值,如果不存在该option 则会创建>>> config.set("db", "db_port", "")
>>> config.set("db", "db_port2", "")
>>> config.write(open("ini", "w"))
>>> exit()
[centos@s202 ~]$ cat -n ini
1 [db]
2 db_host = 127.0.0.1
3 db_port = 70
4 db_user = root
5 db_pass = root
6 host_port = 69
7 db_port2 = 22
8
9 [concurrent]
10 thread = 10
11 processor = 20
12
6、检查section或option是否存在,bool值
import configparser
config = configparser.ConfigParser()
config.has_section("section") #是否存在该section
config.has_option("section", "option") #是否存在该op
7、添加section 和 option
import configparser
config = configparser.ConfigParser()
config.read("ini", encoding="utf-8")
if not config.has_section("default"): # 检查是否存在section
config.add_section("default")
if not config.has_option("default", "db_host"): # 检查是否存在该option
config.set("default", "db_host", "1.1.1.1")
config.write(open("ini", "w"))
[db]
db_host = 127.0.0.1
db_port = 69
db_user = root
db_pass = root [concurrent]
thread = 10
processor = 20 [default]
db_host = 1.1.1.1 运行结果
8、删除section 和 option
import configparser
config = configparser.ConfigParser()
config.read("ini", encoding="utf-8")
config.remove_section("default") #整个section下的所有内容都将删除
config.write(open("ini", "w"))
[db]
db_host = 127.0.0.1
db_port = 69
db_user = root
db_pass = root [concurrent]
thread = 10
processor = 20 运行结果
9、写入文件
以下的几行代码只是将文件内容读取到内存中,进过一系列操作之后必须写回文件,才能生效。
import configparser
config = configparser.ConfigParser()
config.read("ini", encoding="utf-8")
写回文件的方式如下:(使用configparser的write方法)
config.write(open("ini", "w"))
python 的ConfigParser模块的更多相关文章
- 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模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser.ConfigParser和SafeConf ...
- python中configparser模块读取ini文件
python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...
- Python自动化测试 -ConfigParser模块读写配置文件
C#之所以容易让人感兴趣,是因为安装完Visual Studio, 就可以很简单的直接写程序了,不需要做如何配置. 对新手来说,这是非常好的“初体验”, 会激发初学者的自信和兴趣. 而有些语言的开发环 ...
- python封装configparser模块获取conf.ini值
configparser模块是python自带的从文件中获取固定格式参数的模块,因为是python只带的,大家用的应该很多,我觉得这个参数模块比较灵活,添加参数.修改参数.读取参数等都有对应的参数供用 ...
- python中configparser模块的使用
configparser模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 首先要写一个如下所示的配置文件: [DEFAULT] serv ...
- (转)python的ConfigParser模块
原文:https://blog.csdn.net/miner_k/article/details/77857292 如何使用Python3读写INI配置文件-------https://blog.cs ...
- python 配置文件 ConfigParser模块
ConfigParser模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 来看一个好多软件的常见文档格式如下 [DEFAULT] Se ...
随机推荐
- Linux C/C++基础 文件(中)
1.ubuntu cat命令的实现 cat——查看或者合并文件内容 #include<stdio.h> int main(int argc,char* argv[]) { //1.打开文件 ...
- 解析之Apache解析
- 【VS开发】【DSP开发】TCP和UDP数据包结构
TCP (Transport Control Protocol)传输控制协议: 1.TCP数据包的分组格式: A,源端口:标识源端应用进程. B, 目的端口:标识目的端应用进程. C, 序号:在SYN ...
- jenkins pipline 和 jenkinsfile
Jenkins Pipeline(或简称为 "Pipeline")是一套插件,将持续交付的实现和实施集成到 Jenkins 中.Jenkins Pipeline 提供了一套可扩展的 ...
- IIS写权限漏洞和XFF刷票原理
IIS写权限漏洞 PUT写入漏洞 此漏洞主要是因为服务器开启了webdav的组件导致的 1.扫描漏洞,yes,可以PUT: 2.用老兵的工具上传一句话文件test.txt,然后move改名为shell ...
- Exclusive Time of Functions
On a single threaded CPU, we execute some functions. Each function has a unique id between 0 and N- ...
- Selenium在IE浏览器中执行脚本时输入字符太慢问题解决方法
问题描述: IE浏览器中执行Selenium脚本的时候发现输入文本的时候总是一个字符一个字符的输入,并且输入每个字符之间都隔几秒. 解决方案: 将使用的IE浏览器驱动由64位的换成32位的.
- 多模块环境下修改包名Rename directory与Rename package
选中包名->右键Refactor->Rename,如果会弹出的警告框让选择Rename directory和Rename package时,若选择Rename directory,则是只修 ...
- 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题)
layout: post title: 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题) author: "luowentaoaa" c ...
- layui2.5 修改layuicms
雷哥layui2.5版本学习 学习地址: https://www.bilibili.com/video/av59813890/?p=30 注意: 修改layuicms时注意下面是缓存的js, < ...