python之configparser模块详解--小白博客
configparse模块
一、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 常用方法 、获取所用的section节点 # 获取所用的section节点
import configparser
config = configparser.ConfigParser()
config.read("ini", encoding="utf-8")
print(config.sections())
#运行结果
# ['db', 'concurrent'] 、获取指定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'] 、获取指点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 、获取指点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', ''), ('db_user', 'root'), ('db_pass', 'root'), ('host_port', '')] 、修改某个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")) 运行结果
、检查section或option是否存在,bool值 import configparser
config = configparser.ConfigParser()
config.has_section("section") #是否存在该section
config.has_option("section", "option") #是否存在该option
、添加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")) 运行结果
、删除section 和 option import configparser
config = configparser.ConfigParser()
config.read("ini", encoding="utf-8")
config.remove_section("default") #整个section下的所有内容都将删除
config.write(open("ini", "w"))
运行结果
、写入文件 以下的几行代码只是将文件内容读取到内存中,进过一系列操作之后必须写回文件,才能生效。 import configparser
config = configparser.ConfigParser()
config.read("ini", encoding="utf-8")
写回文件的方式如下:(使用configparser的write方法) config.write(open("ini", "w"))
python之configparser模块详解--小白博客的更多相关文章
- python之subprocess模块详解--小白博客
subprocess模块 subprocess是Python 2.4中新增的一个模块,它允许你生成新的进程,连接到它们的 input/output/error 管道,并获取它们的返回(状态)码.这个模 ...
- python之socket模块详解--小白博客
主要是创建一个服务端,在创建服务端的时候,主要步骤如下:创建socket对象socket——>绑定IP地址和端口bind——>监听listen——>得到请求accept——>接 ...
- python之OS模块详解
python之OS模块详解 ^_^,步入第二个模块世界----->OS 常见函数列表 os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows ...
- python之sys模块详解
python之sys模块详解 sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧! sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传 ...
- python中threading模块详解(一)
python中threading模块详解(一) 来源 http://blog.chinaunix.net/uid-27571599-id-3484048.html threading提供了一个比thr ...
- Python中time模块详解
Python中time模块详解 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. ...
- Python的logging模块详解
Python的logging模块详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.日志级别 日志级别指的是产生的日志的事件的严重程度. 设置一个级别后,严重程度 ...
- python configparser模块详解
此模块提供了一个实现基本配置语言的类 首先来看一个非常基本的配置文件,如下所示格式: [DEFAULT] ServerAliveInterval = 45 Compression = yes Comp ...
- python中常用模块详解二
log模块的讲解 Python 使用logging模块记录日志涉及四个主要类,使用官方文档中的概括最为合适: logger提供了应用程序可以直接使用的接口API: handler将(logger创建的 ...
随机推荐
- Java8之Optional类
写在前头 今天再看阿里的Java开发手册,里面异常处理第10条提到这样一个建议. [推荐]防止 NPE ,是程序员的基本修养,注意 NPE 产生的场景:1 ) 返回类型为基本数据类型,return 包 ...
- 使用NOPI写入Excel基础代码
using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.IO; using Sy ...
- [Go] golang的接口合约
接口类型1.接口类型具体描述了一系列方法的集合,实现这些方法的具体类型是这个接口类型的实例2.一个类型如果拥有一个接口需要的所有方法,那么这个类型就实现了这个接口 package main impor ...
- Spring Boot 整合 docker
一.什么是docker ? 简介 Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括VMs ...
- 关于mybatis条件查询 报错:元素内容必须由格式正确的字符数据或标记组成
原查询 select sum(case when age<=16 then 1 else 0 end ) age1, sum(case when age>16 and age<=25 ...
- input中只能写入数字int、float
input 属性 type="number" <input type="number" min="1" max="100&q ...
- arcgis for js学习之Draw类
arcgis for js学习之Draw类 <!DOCTYPE html> <html> <head> <meta http-equiv="Cont ...
- 红米Note 5A完美卡刷开发版获得ROOT超级权限的方法
小米的手机不同手机型号一般情况官方论坛都提供两个不同的系统,大概可分为稳定版和开发版,稳定版没有提供root权限管理,开发版中就支持了root权限,在很多工作的时候我们需要使用的一些功能强大的APP, ...
- 集成Android人脸识别demo分享
本应用来源于虹软人工智能开放平台,人脸识别技术工程如何使用? 1.下载代码 git clone https://github.com/andyxm/ArcFaceDemo.git 2.下载虹软人脸识别 ...
- window64位电脑如何通过VMware Workstation12.5.6安装苹果操作系统 macOS High Sierra 10.13
1.下载 VMware-workstation-full-12.5.6.exe,macOS High Sierra 10.13.iso 2.安装 VMware-workstation时不要选择C盘,因 ...