Python3 configparse模块(配置)
ConfigParser模块在python中是用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section),每个节可以有多个参数(键=值)。
注意:在python 3 中ConfigParser模块名已更名为configparser
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
config.read( 'example.ini' ,encoding = "utf-8" ) """读取配置文件,python3可以不加encoding""" options(section) """sections(): 得到所有的section,并以列表的形式返回""" config.defaults() """defaults():返回一个包含实例范围默认值的词典""" config.add_section(section) """添加一个新的section""" config.has_section(section) """判断是否有section""" print (config.options(section)) """得到该section的所有option""" has_option(section, option) """判断如果section和option都存在则返回True否则False""" read_file(f, source = None ) """读取配置文件内容,f必须是unicode""" read_string(string, source = ’’) """从字符串解析配置数据""" read_dict(dictionary, source = ’’) """从词典解析配置数据""" get(section, option, * , raw = False , vars = None [, fallback]) """得到section中option的值,返回为string类型""" getint(section,option) """得到section中option的值,返回为int类型""" getfloat(section,option) """得到section中option的值,返回为float类型""" getboolean(section, option) """得到section中option的值,返回为boolean类型""" items(raw = False , vars = None ) """和items(section, raw=False, vars=None):列出选项的名称和值""" set (section, option, value) """对section中的option进行设置""" write(fileobject, space_around_delimiters = True ) """将内容写入配置文件。""" remove_option(section, option) """从指定section移除option""" remove_section(section) """移除section""" optionxform(option) """将输入文件中,或客户端代码传递的option名转化成内部结构使用的形式。默认实现返回option的小写形式;""" readfp(fp, filename = None ) """从文件fp中解析数据""" |
生成configparser文件实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import configparser #配置文件 config = configparser.ConfigParser() """生成configparser配置文件 ,字典的形式""" """第一种写法""" config[ "DEFAULT" ] = { 'ServerAliveInterval' : '45' , 'Compression' : 'yes' , 'CompressionLevel' : '9' } """第二种写法""" config[ 'bitbucket.org' ] = {} config[ 'bitbucket.org' ][ 'User' ] = 'hg' """第三种写法""" config[ 'topsecret.server.com' ] = {} topsecret = config[ 'topsecret.server.com' ] topsecret[ 'Host Port' ] = '50022' # mutates the parser topsecret[ 'ForwardX11' ] = 'no' # same here config[ 'DEFAULT' ][ 'ForwardX11' ] = 'yes' """写入后缀为.ini的文件""" with open ( 'example.ini' , 'w' ) as configfile: config.write(configfile) |
运行结果:
1
2
3
4
5
6
7
8
9
10
11
12
|
[DEFAULT] serveraliveinterval = 45 compression = yes compressionlevel = 9 forwardx11 = yes [bitbucket.org] user = hg [topsecret.server.com] host port = 50022 forwardx11 = no |
读取configparser配置文件的实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import configparser #配置文件 config = configparser.ConfigParser() config.read( "example.ini" ) print ( "所有节点==>" ,config.sections()) print ( "包含实例范围默认值的词典==>" ,config.defaults()) for item in config[ "DEFAULT" ]: print ( "循环节点topsecret.server.com下所有option==>" ,item) print ( "bitbucket.org节点下所有option的key,包括默认option==>" ,config.options( "bitbucket.org" )) print ( "输出元组,包括option的key和value" ,config.items( 'bitbucket.org' )) print ( "bitbucket.org下user的值==>" ,config[ "bitbucket.org" ][ "user" ]) #方式一 topsecret = config[ 'bitbucket.org' ] print ( "bitbucket.org下user的值==>" ,topsecret[ "user" ]) #方式二 print ( "判断bitbucket.org节点是否存在==>" , 'bitbucket.org' in config) print ( "获取bitbucket.org下user的值==>" ,config.get( "bitbucket.org" , "user" )) print ( "获取option值为数字的:host port=" ,config.getint( "topsecret.server.com" , "host port" )) |
运行结果
1
2
3
4
5
6
7
8
9
10
11
12
13
|
所有节点 = = > [ 'bitbucket.org' , 'topsecret.server.com' ] 包含实例范围默认值的词典 = = > OrderedDict([( 'serveraliveinterval' , '45' ), ( 'compression' , 'yes' ), ( 'compressionlevel' , '9' ), ( 'forwardx11' , 'yes' )]) 循环节点topsecret.server.com下所有option = = > serveraliveinterval 循环节点topsecret.server.com下所有option = = > compression 循环节点topsecret.server.com下所有option = = > compressionlevel 循环节点topsecret.server.com下所有option = = > forwardx11 bitbucket.org节点下所有option的key,包括默认option = = > [ 'user' , 'serveraliveinterval' , 'compression' , 'compressionlevel' , 'forwardx11' ] 输出元组,包括option的key和value [( 'serveraliveinterval' , '45' ), ( 'compression' , 'yes' ), ( 'compressionlevel' , '9' ), ( 'forwardx11' , 'yes' ), ( 'user' , 'hg' )] bitbucket.org下user的值 = = > hg bitbucket.org下user的值 = = > hg 判断bitbucket.org节点是否存在 = = > True 获取bitbucket.org下user的值 = = > hg 获取option值为数字的:host port = 50022 |
删除配置文件section和option的实例(默认分组有参数时无法删除,但可以先删除下面的option,再删分组)
1
2
3
4
5
6
7
8
|
import configparser #配置文件 config = configparser.ConfigParser() config.read( "example.ini" ) config.remove_section( "bitbucket.org" ) """删除分组""" config.remove_option( "topsecret.server.com" , "host port" ) """删除某组下面的某个值""" config.write( open ( 'example.ini' , "w" )) |
运行结果
1
2
3
4
5
6
7
8
|
[DEFAULT] serveraliveinterval = 45 compression = yes compressionlevel = 9 forwardx11 = yes [topsecret.server.com] forwardx11 = no |
配置文件的修改实例
1
2
3
4
5
6
7
8
9
|
"""修改""" import configparser config = configparser.ConfigParser() config.read( "example.ini" ) config.add_section( "new_section" ) """新增分组""" config. set ( "DEFAULT" , "compressionlevel" , "110" ) """设置DEFAULT分组下compressionlevel的值为110""" config.write( open ( 'example.ini' , "w" )) |
运行结果
1
2
3
4
5
6
7
8
9
10
|
[DEFAULT] serveraliveinterval = 45 compression = yes compressionlevel = 110 forwardx11 = yes [topsecret.server.com] forwardx11 = no [new_section] |
Python3 configparse模块(配置)的更多相关文章
- 【转】Python3 configparse模块(配置)
[转]Python3 configparse模块(配置) ConfigParser模块在python中是用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(s ...
- python configparse模块&xml模块
configparse模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. [DEFAULT] serveraliveinterval = ...
- python模块: hashlib模块, configparse模块, logging模块,collections模块
一. hashlib模块 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用 ...
- 14 ConfigParse模块
1.ConfigParse模块的基本概念 此模块用于生成和修改常见配置文档. ConfigParser 是用来读取配置文件的包. 配置文件的格式如下:中括号“[ ]”内包含的为section.sect ...
- configParse模块
一.配置文件简介 在各种程序里面都有配置文件,为了对配置文件进行操作. python中引入了configParse模块进行操作. 配置数值类型: 配置文件中,我们看到的bool型,整数型,在我们操作的 ...
- 0423 hashlib模块、logging模块、configparse模块、collections模块
一.hashlib模块补充 1,密文验证 import hashlib #引入模块 m =hashlib.md5() # 创建了一个md5算法的对象 m.update(b') print(m.hexd ...
- Python3 logging 模块
Python3 logging模块 日志模块: 用于便捷记录日志且线程安全的模块 CRITICAL = 50 FATAL = CRITICAL ERROR = 40 WARNING = 30 WARN ...
- Python进阶-XVV hashlib模块、configparse模块、logging模块
1.配置相关的configparse模块 配置文件如何组织?python中常见的是将配置文件写成py,然后引入该模块即可.优点是方便访问. 但是也有用类似windows中的ini文件的配置文件,了解即 ...
- python学习-58 configparse模块
configparse模块 1.生成文件 import configparser # 配置解析模块 config = configparser.ConfigParser() # config = { ...
随机推荐
- MT【5】蝴蝶效应:一道递推式为二次的数列
评:蝴蝶效应[蝴蝶效应(The Butterfly Effect)是指在一个动力系统中,初始条件下微小的变化能带动整个系统的长期的巨 ...
- 【BZOJ1558】等差数列(线段树)
[BZOJ1558]等差数列(线段树) 题面 BZOJ 题解 可以说这道题已经非常毒瘤了 怎么考虑询问操作? 如果直接将一段数分解为等差数列? 太麻烦了.... 考虑相邻的数做差, 这样等差数列变为了 ...
- 【UR #17】滑稽树前做游戏
假装看懂的样子 假装会做的样子 UOJ Round #17 题解 加上一个(t-w)^c,c是和i相连的点的度数 是一个多项式的话可以归纳证明 一些具体实现: 多项式存储,保留t,y, f=ai*t^ ...
- c输出格式
#include <stdio.h> #include <stdlib.h> #include <math.h> int main() { //取整 printf( ...
- Mysq中的流程控制语句的用法
这篇博客主要是总结一下Mysq中的流程控制语句的用法,主要是:CASE,IF,IFNULL,NULLIF 1.case CASE value WHEN [compare-value] THEN res ...
- 约会 音频mm教你追女孩
微信吧地址发给他人. 美团提前选好环境然后提前打电话订购一个位置. 微博作用是为:更多的谈资.热搜 ,最近上榜的话题说. 打车软件: 地图: 2.外表: 下澡,指甲,胡子,发型,适合服装.发型和服装搭 ...
- VS2010程序崩溃,报错Unhandled exception at **.exe:0xC0000005: Access violation reading location 0x000000008899.
最近被派到另外一个组支援,从而从Linux下开发暂转到Windows下开发,个人觉得Windows自己搞的一套并不那么完美,坑多. 网文可能出现的原因: 未处理的异常: 0xC0000005: 读取位 ...
- Linux系统下yum镜像源环境部署记录
之前介绍了Linux环境下本地yum源配置方法,不过这个是最简单最基础的配置,在yum安装的时候可能有些软件包不够齐全,下面说下完整yun镜像源系统环境部署记录(yum源更新脚本下载地址:https: ...
- Prometheus MySQL_exporter
MySQL Exporter mysqld_exporter是用来搜集mysql的性能指标的,适用于mysql5.5及其以上版本 程序安装 下载地址:https://prometheus.io/dow ...
- H5新特性之拖拽文件
H5新增了drag事件,在H5中拖拽是十分常见的. 可以拖拽的分为页面内的和页面外的 页面内的一般默认可以拖拽的是img和a标签 页面外的常指的是文件 上代码吧~ let zoom = documen ...