一、ConfigParser简介

ConfigParser 是用来读取配置文件的包。配置文件的格式如下:中括号“[ ]”内包含的为section。section 下面为类似于key-value 的配置内容。

[db]
db_host = 127.0.0.1
db_port =
db_user = root
db_pass = root
host_port = [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的值,如果不存在则会出创建

# 修改某个option的值,如果不存在该option 则会创建
import configparser
config = configparser.ConfigParser()
config.read("ini", encoding="utf-8")
config.set("db", "db_port", "") #修改db_port的值为69
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

运行结果

6、检查section或option是否存在,bool值

import configparser
config = configparser.ConfigParser()
config.has_section("section") #是否存在该section
config.has_option("section", "option") #是否存在该option

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模块的更多相关文章

  1. python封装configparser模块获取conf.ini值(优化版)

    昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...

  2. python中confIgparser模块学习

    python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...

  3. Python中ConfigParser模块应用

    Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser.ConfigParser和SafeConf ...

  4. python中configparser模块读取ini文件

    python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...

  5. python 的ConfigParser模块

    Python 之ConfigParser模块 一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.sect ...

  6. Python自动化测试 -ConfigParser模块读写配置文件

    C#之所以容易让人感兴趣,是因为安装完Visual Studio, 就可以很简单的直接写程序了,不需要做如何配置. 对新手来说,这是非常好的“初体验”, 会激发初学者的自信和兴趣. 而有些语言的开发环 ...

  7. python封装configparser模块获取conf.ini值

    configparser模块是python自带的从文件中获取固定格式参数的模块,因为是python只带的,大家用的应该很多,我觉得这个参数模块比较灵活,添加参数.修改参数.读取参数等都有对应的参数供用 ...

  8. python中configparser模块的使用

    configparser模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 首先要写一个如下所示的配置文件: [DEFAULT] serv ...

  9. (转)python的ConfigParser模块

    原文:https://blog.csdn.net/miner_k/article/details/77857292 如何使用Python3读写INI配置文件-------https://blog.cs ...

  10. python 配置文件 ConfigParser模块

    ConfigParser模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 来看一个好多软件的常见文档格式如下 [DEFAULT] Se ...

随机推荐

  1. cmd的变量总结

    转自:https://blog.csdn.net/flyoutsan/article/details/52811095 cmd变量通过set设置变量,通过可以使用set /?查看有关变量的帮助文档. ...

  2. Swift代理的使用

    Swift代理的使用 协议规定了用来实现某一特定功能所必需的方法和属性. 任意能够满足协议要求的类型被称为遵循(conform)这个协议. 类,结构体或枚举类型都可以遵循协议,并提供具体实现来完成协议 ...

  3. Linux Mysql数据库安全配置

    Linux  Mysql数据库安全配置 目录: 1.修改mysql管理员账号root的密码(2种方法) 2.修改mysql管理员账号root 3.mysql管理员root账号密码遗忘解决办法(2种方法 ...

  4. win8.1 AMD 屏幕亮度无法调整

    lenovo z465  AMD处理器. win8.1 pro系统   屏幕亮度无法调整解决办法:   1:当然是先去本地服务里禁用"Sensor Monitoring Service&qu ...

  5. MVC Remote 服务器验证

    用此验证必须在Controller中编写返回值为JsonResult的Action public JsonResult CheckUserName(string UserName) { EFHelpe ...

  6. bat(批处理)命令(tomcat 7.0.75 startup.bat 命令集)

    本文主要介绍tomcat 7.0.75中startup.bat(位置:tomcat目录\bin)中涉及到的bat命令,为tomcat源码研究做准备. startup.bat中涉及到的bat命令如下: ...

  7. LeetCode算法题-N-ary Tree Postorder Traversal(Java实现)

    这是悦乐书的第269次更新,第283篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第136题(顺位题号是590).给定一个n-ary树,返回其节点值的后序遍历.例如,给定 ...

  8. .NET MVC全局异常处理(二)

    目录 .NET MVC全局异常处理(二) MVC过滤器Filter .NET MVC全局异常处理(二) 对上节的内容进行了补充 MVC过滤器Filter MVC有四种过滤器:Authorization ...

  9. 通过ip查询自己电脑的共享文件夹

    查看电脑所有的共享文件或文件夹的三种方法如下: 方法一. 右键点击网上邻居,点击属性进入网上邻居属性页面. 选中本地连接,在窗口的左下方有详细信息,可以看到内网IP,记住IP地址. 直接在地址栏输入记 ...

  10. web框架开发-模板层

    你可能已经注意到我们在例子视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python代码之中. def current_datetime(request): now = datet ...