1.基本的读取配置文件

-read(filename) 直接读取ini文件内容

-sections() 得到所有的section,并以列表的形式返回

-options(section) 得到该section的所有option

-items(section) 得到该section的所有键值对

-get(section,option) 得到section中option的值,返回为string类型

-getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。

2.基本的写入配置文件

-add_section(section) 添加一个新的section

-set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件。

3.基本例子

test.conf

[sec_a] 
a_key1 = 20 
a_key2 = 10 
   
[sec_b] 
b_key1 = 121 
b_key2 = b_value2 
b_key3 = $r 
b_key4 = 127.0.0.1

parse_test_conf.py

import ConfigParser 
cf = ConfigParser.ConfigParser() 
#read config 
cf.read("test.conf"
# return all section 
secs = cf.sections() 
print 'sections:', secs 
   
opts = cf.options("sec_a"
print 'options:', opts 
   
kvs = cf.items("sec_a"
print 'sec_a:', kvs 
   
#read by type 
str_val = cf.get("sec_a""a_key1"
int_val = cf.getint("sec_a""a_key2"
   
print "value for sec_a's a_key1:", str_val 
print "value for sec_a's a_key2:", int_val 
   
#write config 
#update value 
cf.set("sec_b""b_key3""new-$r"
#set a new value 
cf.set("sec_b""b_newkey""new-value"
#create a new section 
cf.add_section('a_new_section'
cf.set('a_new_section''new_key''new_value'
   
#write back to configure file 
cf.write(open("test.conf""w"))

得到终端输出:

sections: ['sec_b''sec_a'
options: ['a_key1''a_key2'
sec_a: [('a_key1'"i'm value"), ('a_key2', '22')] 
value for sec_a's a_key1: i'm value 
value for sec_a's a_key2: 22

更新后的test.conf

[sec_b] 
b_newkey = new-value 
b_key4 = 127.0.0.1 
b_key1 = 121 
b_key2 = b_value2 
b_key3 = new-$r 
   
[sec_a] 
a_key1 = i'm value 
a_key2 = 22 
   
[a_new_section] 
new_key = new_value

4.Python 的ConfigParser Module中定义了3个类对INI文件进行操作。分别是RawConfigParser、ConfigParser、 SafeConfigParser。RawCnfigParser是最基础的INI文件读取类,ConfigParser、 SafeConfigParser支持对%(value)s变量的解析。

设定配置文件test2.conf

[portal] 
url = http://%(host)s:%(port)s/Portal 
host = localhost 
port = 8080

使用RawConfigParser:

import ConfigParser 
  
cf = ConfigParser.RawConfigParser() 
  
print "use RawConfigParser() read" 
cf.read("test2.conf"
print cf.get("portal""url"
  
print "use RawConfigParser() write" 
cf.set("portal""url2""%(host)s:%(port)s"
print cf.get("portal""url2")

得到终端输出:

use RawConfigParser() read 
http://%(host)s:%(port)s/Portal 
use RawConfigParser() write 
%(host)s:%(port)s

改用ConfigParser:

import ConfigParser 
  
cf = ConfigParser.ConfigParser() 
  
print "use ConfigParser() read" 
cf.read("test2.conf"
print cf.get("portal""url"
  
print "use ConfigParser() write" 
cf.set("portal""url2""%(host)s:%(port)s"
print cf.get("portal""url2")

得到终端输出:

use ConfigParser() read 
http://localhost:8080/Portal 
use ConfigParser() write 
localhost:8080

改用SafeConfigParser:

import ConfigParser 
  
cf = ConfigParser.SafeConfigParser() 
  
print "use SafeConfigParser() read" 
cf.read("test2.conf"
print cf.get("portal""url"
  
print "use SateConfigParser() write" 
cf.set("portal""url2""%(host)s:%(port)s"
print cf.get("portal""url2")

得到终端输出(效果同ConfigParser):

use SafeConfigParser() read 
http://localhost:8080/Portal 
use SateConfigParser() write 
localhost:8080

转载于:https://blog.51cto.com/ponyjia/1752194

Python 解析配置模块之ConfigParser详解相关推荐

  1. python中selenium模块驱动谷歌详解

    python中selenium模块驱动谷歌详解 Selenium的介绍.配置和调用 Selenium(浏览器自动化测试框架) 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中 ...

  2. Python中random模块生成随机数详解

    Python中random模块生成随机数详解 本文给大家汇总了一下在Python中random模块中最常用的生成随机数的方法,有需要的小伙伴可以参考下 Python中的random模块用于生成随机数. ...

  3. python中xlrd模块的使用详解

    python中xlrd模块的使用详解 两个问题: 什么是xlrd模块? 为什么使用xlrd模块? 1.什么是xlrd模块? python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读 ...

  4. python hashlib_Python hashlib模块实例使用详解

    这篇文章主要介绍了Python hashlib模块实例使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 hashlib模块主要的作用: 加密保 ...

  5. python中re模块的span,详解Python正则表达式re模块

    正则是处理字符串最常用的方法,我们编码中到处可见正则的身影. 正则大同小异,python 中的正则跟其他语言相比略有差异: 1.替换字符串时,替换的字符串可以是一个函数 2.split 函数可以指定分 ...

  6. 用于生成随机数的python标准库模块是_详解Python基础random模块随机数的生成

    详解Python基础random模块随机数的生成 来源:中文源码网    浏览: 次    日期:2019年11月5日 [下载文档:  详解Python基础random模块随机数的生成.txt ] ( ...

  7. Python编程——pickle模块的使用详解(附实例)

    1.  模块的介绍 在介绍pickle模块之前,我们先了解一下python中的模块以及模块的分类,可以让我们对模块有更深入的了解. (1) 模块是什么: Python 模块(Module),是一个 P ...

  8. Python之Re模块匹配正则表达式详解

    目录 前言 1. re.match方法 2. re.search方法 3. 检索和替换方法re.sub 4. re.compile方法 5. re.findall方法 6. re.finditer方法 ...

  9. Python 正则re模块之findall()详解

    1. 先说一下findall()函数的两种表示形式 import re kk = re.compile(r'\d+') kk.findall('one1two2three3four4') #[1,2, ...

最新文章

  1. SQL Server查询备份日期和备份设备名
  2. ASP.NET Web Services Tutorial
  3. 在Debian/Ubuntu上面安装升级nginx到最新版
  4. kill -3 获取threaddump信息---转载
  5. K8S 基于NFS实现文件集群间共享
  6. linux location root访问文件夹404_如何使网站支持https访问?nginx配置https证书
  7. mysql数据横表变成竖表_MySQL中横表和竖表相互转换
  8. linux下docker安装步骤_linux下安装mysql8步骤
  9. Ionic 框架宣布 2019 年将正式支持 Vue 和 React
  10. SQL2005数据库连接
  11. 4.Unix工作环境
  12. Rust跨界前端全攻略
  13. python 简单的绘图
  14. revit 转换ifc_导出 IFC 文件以使用 BIM 软件进行编辑
  15. 西狐爱墙:为中国祈福!
  16. PPAPI nacl_sdk安装
  17. python chardet_【已解决】windows下,安装python的chardet
  18. 《系统分析与设计》课程设计——医院门诊信息管理查询系统
  19. 为什么网站打得开,却ping不通, 网站却打得开
  20. github下载release连接失败解决方法 亲测有效

热门文章

  1. VirtualHost 的配置
  2. Oracle RAC一节点宕机导致另一节点HANG的问题分析
  3. 安装zeromq以及zeromq的python示例
  4. 【JS基础】类型转换——不同数据类型比较
  5. LDA-math-MCMC 和 Gibbs Sampling
  6. VMware虚拟化技术培训(11) 小结
  7. redhat5.5安装oracle10g出现问题
  8. 『第27天』Sunos(二)
  9. WebSpider的编码问题(乱码)浅析
  10. English in 999