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

1
2
3
4
5
6
7
8
9
[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

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
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"))

得到终端输出:

1
2
3
4
5
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

1
2
3
4
5
6
7
8
9
10
11
12
13
[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

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

使用RawConfigParser:

1
2
3
4
5
6
7
8
9
10
11
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")

得到终端输出:

1
2
3
4
use RawConfigParser() read 
http://%(host)s:%(port)s/Portal 
use RawConfigParser() write 
%(host)s:%(port)s

改用ConfigParser:

1
2
3
4
5
6
7
8
9
10
11
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")

得到终端输出:

1
2
3
4
use ConfigParser() read 
http://localhost:8080/Portal 
use ConfigParser() write 
localhost:8080

改用SafeConfigParser:

1
2
3
4
5
6
7
8
9
10
11
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):

1
2
3
4
use SafeConfigParser() read 
http://localhost:8080/Portal 
use SateConfigParser() write 
localhost:8080

http://www.qytang.com/
http://www.qytang.com/cn/list/29/
http://www.qytang.com/cn/list/28/358.htm
http://www.qytang.com/cn/list/41/
http://www.qytang.com/cn/list/37/
http://www.qytang.com/cn/list/46/
http://www.qytang.com/cn/page/19.htm
http://www.qytang.com/cn/list/32/
http://www.qytang.com/cn/list/28/
http://www.qytang.com/cn/list/25/
http://www.qytang.com/cn/list/28/625.htm
http://www.qytang.com/cn/list/28/612.htm
http://www.qytang.com/cn/list/28/611.htm

Python 解析配置模块之ConfigParser详解-乾颐堂的更多相关文章

  1. python 多继承详解-乾颐堂

    1 2 3 4 5 6 7 8 9 10 class A(object):    # A must be new-style class    def __init__(self):     prin ...

  2. python时间处理详解-乾颐堂

    1.获取当前时间的两种方法: import datetime,time now = time.strftime("%Y-%m-%d %H:%M:%S") print now now ...

  3. HTTP 499状态码 nginx下499错误详解-乾颐堂

    日志记录中HTTP状态码出现499错误有多种情况,我遇到的一种情况是nginx反代到一个永远打不开的后端,就这样了,日志状态记录是499.发送字节数是0. 老是有用户反映网站系统时好时坏,因为线上的产 ...

  4. Linux ls命令详解-乾颐堂CCIE

      ls命令用法举例: 例一:列出/home文件夹下的所有文件和目录的详细资料: 1 ls -l -R /home 命令参数之前要有一短横线“-”, 上面的命令也可以这样写: 1 ls -lR /ho ...

  5. xargs在linux中的使用详解-乾颐堂

    xargs在linux中是个很有用的命令,它经常和其他命令组合起来使用,非常的灵活. xargs是给命令传递参数的一个过滤器,也是组合多个命令的一个工具.它把一个数据流分割为一些足够小的块,以方便过滤 ...

  6. linux sed命令详解-乾颐堂CCIE

    简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的 ...

  7. nginx内置变量详解-乾颐堂

    nginx的配置文件中可以使用的内置变量以美元符$开始,也有人叫全局变量.其中,部分预定义的变量的值是可以改变的. $arg_PARAMETER 这个变量值为:GET请求中变量名PARAMETER参数 ...

  8. python中argparse模块用法实例详解

    python中argparse模块用法实例详解 这篇文章主要介绍了python中argparse模块用法,以实例形式较为详细的分析了argparse模块解析命令行参数的使用技巧,需要的朋友可以参考下 ...

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

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

随机推荐

  1. 对SQLite数据库操作 操作db文件

    sqlite数据库就是一个DB文件.  程序每操作一次数据库都要读一次 .DB  文件 .  这个文件就是这个SQLite数据库. 如果需要依赖包的可以联系我 工具类: package com.hot ...

  2. 皆在FPGA之外

    最近做电力方面的项目,由于跨行业,所以很长一段时间都在做前期准备工作. 项目设计前应尽量做到面面俱到,否则会在项目设计中遇到下面大概率问题: 性能不满足需求,然后为了提升性能,资源又成了瓶颈: 功能设 ...

  3. Python库-re(正则表达式)

    re库是python的一个标准库,不需要自己用pip额外下载,直接调用即可. 下面介绍以下库中函数的作用. 1.re.compile(patter, flags=0) patter是一个正则表达式字符 ...

  4. java 检测代理IP是否准确

    我这里提供2个方法都可以实现:第一个是createIPAddress()和convertStreamToString() import java.io.IOException; import java ...

  5. node制作代理服务器

    下面代码实现的功能是这样的: 首先创建一个HTTP服务器,当服务器接收到客户端的请求后,向"www.taobao.com"网站请求数据,当从该网站接受到的响应数据后,将响应数据发送 ...

  6. List、Set、Map下各类型的对比

    1.List和Set: List: 元素有放入顺序,元素可重复,查找效率高,插入删除效率低: Set: 元素无放入顺序,元素不可重复,(元素虽然无顺序,但元素在Set中的位置是由该元素的HashCod ...

  7. PCI、PCI-x,PCI-E兼容以及他们之间的区别详细图解

    一.PCI PCI接口分为32bit和64bit两种,32bit就是一般台式机使用的普通的pci接口(图一.图三),64bit接口比32bit接口长一些一般只出现在服务器上(图四.图五).32bit和 ...

  8. HALCON形状匹配(转)

    LIntExport Herror create_shape_model( const Hobject&  Template ,  //reduce_domain后的模板图像 Hlong  N ...

  9. linux查找目录下的所有文件中是否含有某个字符串 <zhuan>

    查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" 查找目录下的所有文件中是否含有某个字符串,并且只打印出文件名 find .|xar ...

  10. Sublime Text:初学者不知道的那些事

    来源:Duchessjojo@译言 我是Sublime Text代码编辑器的忠实粉丝.我和诸多Mac程序员一样,最初使用的是Textmate代码编辑器.在Sublime Text 2发行后,我才开始转 ...