前言
  处理配置文件做增、删、改、查 操作。

配置文件的格式如下:“[ ]”包含的为 section,section 下面为类似于 key - value 的配置内容; configparser 默认支持 ‘=’ ‘:’ 两种分隔。

 [DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes [bitbucket.org]
User = Tom [topsecret.com]
Port: 50022
ForwardX11: no

configparser 常用方法

1.使用 configparser 首先需要初始化实例,并读取配置文件:

 >>> import configparser
>>> config = configparser.ConfigParser() # 注意大小写
>>> config.read("config.ini") # 配置文件的路径
["config.ini"]

2.获取所有 sections

 >>> config.sections()
['bitbucket.org', 'topsecret.com'] # 注意会过滤掉[DEFAULT]

3.获取指定 section 的 keys & values

 >>> config.items('topsecret.com')
>>>> [('port', ''), ('forwardx11', 'no')] # 注意items()返回的字符串会全变成小写

4.获取指定 section 的 keys

 >>> config.options('topsecret.com')
['Port', 'ForwardX11'] >>> for option in config['topsecret.com']:
... print(option)
Port
ForwardX11

5.获取指定 key 的 value

 >>> config['bitbucket.org']['User']
'Tom'
>>> config.get('bitbucket.org', 'User')
'Tom'
>>> config.getint('topsecret.com', 'Port')
50022

6.检查

 >>> 'DEFAULT' in config
True
>>> 'test' in config['section_test']
False
>>> 'Tom' in config['bitbucket.org']['User']
True >>> config.has_section('bitbucket.org')
True
>>> config.has_option('section_test', 'test')
False

7.添加

 >>> config.add_section('Section_1')
>>> config.set('Section_1', 'key_1', 'value_1') # 注意键值是用set()方法
>>> config.write(open('config.ini', 'w')) # 一定要写入才生效

8.删除

 >>> config.remove_option('Section_1', 'key_1')
True
>>> config.remove_section('Section_1')
True
>>> config.clear() # 清空除[DEFAULT]之外所有内容
>>> config.write(open('config.ini', 'w'))

9.关于 [DEFAULT]:一般包含 ini 格式配置文件的默认项,所以 configparser 部分方法会自动跳过这个 section 。
前面已经提到 sections() 是获取不到的,还有删除方法对 [DEFAULT] 也无效:

 >>> config.remove_section('DEFAULT')
False
>>> config.clear()
>>> 'DEFAULT' in config
True
>>> 'ForwardX11' in config['DEFAULT']
True
>>> config.sections()
[]

但指定删除和修改 [DEFAULT] 里的 keys & values 是可以的:

 >>> config.remove_option('DEFAULT', 'ForwardX11')
True
>>> config.set('DEFAULT', 'ForwardX11','no')
>>> config['DEFAULT']['ForwardX11']
'no'

还有个特殊的是,has_section() 也无效,可以和 in 区别使用

 >>> config.has_section('DEFAULT')
False
>>> 'DEFAULT' in config
True

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. call、apply、bind三者的区别

    先构造函数let xiaowang={ name1:"小王", age:", sex:"男", say:function(){ console.log ...

  2. 微信小程序: rpx与px,rem相互转换

    官方上规定屏幕宽度为20rem,规定屏幕宽为750rpx,则1rem=750/20rpx. 微信官方建议视觉稿以iPhone 6为标准:在 iPhone6 上,屏幕宽度为375px,共有750个物理像 ...

  3. URL to load resources from the classpath in Java

    In Java, you can load all kinds of resources using the same API but with different URL protocols: fi ...

  4. 分析图第二讲导出图片和后期PS5.12

    导出渲染的白模加上EXTRATEX.再导出一张“消隐”样式的模型图片.就是线稿图. 再导出一张着色显示图,并去掉边线.,用于后期PS选择范围用. 把这几张图全都导入ps. 渲染图的阴影面是灰色的,示例 ...

  5. 逆袭之旅DAY.XIA.Object中常用方法

    2018-07-31

  6. shell test判断命令

    判断命令test 使用test命令可以对文件,字符串等进行测试,一般配合控制语句使用,如while,if,case "字符串测试"   test str1==str2 测试字符串是 ...

  7. windows bat发布成服务

    删除服务 删除名为"WINDOWS MANAGEMONT INSTALLER"的系统服务 sc delete "windows managemont Installer& ...

  8. Win10系列:JavaScript小球运动示例

    通过前面内容的学习,相信读者已经对开发基于JavaScript的Windows应用商店应用有了一定的了解,本小节通过一个小球运动的示例来介绍如何新建一个JavaScript的Windows应用商店项目 ...

  9. SignalR NuGet程序包

    最近公司有一个边看直播边聊天的需求,直播好搞,直接用腾讯的小直播,组装推流和播放地址,把推流地址拿出去就OK,只要一推流,就可以使用播放地址观看直播,看完后通过webclient去异步下载直播的视频到 ...

  10. 泛型算法,排序的相关操作,lower_bound、upper_bound、equal_range

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...