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

配置文件的格式如下:“[ ]”包含的为 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. 使用antd-mobile的ImagePicker组件实现图片的上传

    这篇文章主要是记录一下在开发钉钉微应用时,实现图片上传及显示功能的过程. 这个项目用的dingyou-dingtalk-mobile这个脚手架,可直接在NowaGui上创建.这是一个关于钉钉微应用的脚 ...

  2. 牛客练习赛32-D-MST+tarjin割边

    链接:https://ac.nowcoder.com/acm/contest/272/D来源:牛客网 题目描述 小p和他的朋友约定好去游乐场游玩,但是他们到了游乐场后却互相找不到对方了. 游乐场可以看 ...

  3. 函数使用四:采购发票MIRO BAPI_INCOMINGINVOICE_CREATE

    1. 业务处理(transaction)字段选择: 创建后续借记(subsequent debit)            ItemData                     DE_CRE_IN ...

  4. MySQL压力测试(1)-mysqlslap

    mysqlslap是从MySQL的5.1.4版开始就开始官方提供的压力测试工具.通过模拟多个并发客户端并发访问MySQL来执行压力测试,同时提供了较详细的SQL执行数据性能报告,并且能很好的对比多个存 ...

  5. JAVA OCR图片识别

    今天闲来无聊,尝试了一下OCR识别,尝试了以下三种方案: 1.直接使用业界使用最广泛的Tesseract-OCR. Tesseract项目最初由惠普实验室支持,1996年被移植到Windows上,19 ...

  6. SpringBoot 使用Thymeleaf解决静态页面跳转问题

    参考:springboot配置跳转html页面 1,首先在pom文件中引入模板引擎jar包 <dependency> <groupId>org.springframework. ...

  7. mybatis与spring的整合(代码实现)

    mybatis与spring的整合(代码实现) 需要jar包: mybatis核心包:依赖包:log4j包:spring croe;beans;tx;aop;aspects;context;expre ...

  8. IIS隐藏版本号教程(Windows Server 2003)

    1.下载Urlscan https://www.microsoft.com/en-us/search/DownloadResults.aspx?q=URLScan(总下载页面) https://dow ...

  9. 转 Deep Learning for NLP 文章列举

    原文链接:http://www.xperseverance.net/blogs/2013/07/2124/   大部分文章来自: http://www.socher.org/ http://deepl ...

  10. Ubuntu 14.04下如何更换更新源(更新为163源)

    之前的安装ubuntu桌面版的之后安装yum,vim等会遇到一些问题, 比如:Ubuntu 14.04下如何更换更新源(更新为163源) 解决: http://jingyan.baidu.com/ar ...