Python中的ConfigParser模块使用详解

   这篇文章主要介绍了Python中的ConfigParser模块的使用,ConfigParser模块主要被用来读写配置模块,需要的朋友可以参考下

  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.基本例子

  ?

1
2
3
4
5
6
7
8
9
10

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

  ?

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

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

  得到终端输出:

  ?

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

时间: 2024-08-20 15:56:49

Python中的ConfigParser模块使用详解的相关文章

Python中的zipfile模块使用详解

  这篇文章主要介绍了Python中的zipfile模块使用详解,zipfile模块是用来操作zip文件,需要的朋友可以参考下 zip文件格式是通用的文档压缩标准,在ziplib模块中,使用ZipFile类来操作zip文件,下面具体介绍一下: class zipfile.ZipFile(file[, mode[, compression[, allowZip64]]]) 创建一个ZipFile对象,表示一个zip文件.参数file表示文件的路径或类文件对象(file-like object);参

Python中的深拷贝和浅拷贝详解

  这篇文章主要介绍了Python中的深拷贝和浅拷贝详解,本文讲解了变量-对象-引用.可变对象-不可变对象.拷贝等内容,需要的朋友可以参考下 要说清楚Python中的深浅拷贝,需要搞清楚下面一系列概念: 变量-引用-对象(可变对象,不可变对象)-切片-拷贝(浅拷贝,深拷贝) [变量-对象-引用] 在Python中一切都是对象,比如说:3, 3.14, 'Hello', [1,2,3,4],{'a':1}...... 甚至连type其本身都是对象,type对象 Python中变量与C/C++/Ja

Python中的choice()方法使用详解

 这篇文章主要介绍了Python中的choice()方法使用详解,是Python入门中的基础知识,需要的朋友可以参考下     choice()方法从一个列表,元组或字符串返回一个随机项. 语法 以下是choice()方法的语法: ? 1 choice( seq ) 注意:此函数是无法直接访问的,所以我们需要导入random模块,然后我们需要使用random对象来调用这个函数. 参数 seq -- 这可能是一个列表,元组或字符串... 返回值 该方法返回一个随机项. 例子 下面的例子显示了cho

Python中的变量和作用域详解_python

作用域介绍 python中的作用域分4种情况: L:local,局部作用域,即函数中定义的变量: E:enclosing,嵌套的父级函数的局部作用域,即包含此函数的上级函数的局部作用域,但不是全局的: G:globa,全局变量,就是模块级别定义的变量: B:built-in,系统固定模块里面的变量,比如int, bytearray等. 搜索变量的优先级顺序依次是:作用域局部>外层作用域>当前模块中的全局>python内置作用域,也就是LEGB. x = int(2.9) # int bu

Python中的推导式使用详解

  这篇文章主要介绍了Python中的推导式使用详解,本文分别讲解了列表推导式.字典推导式.集合推导式使用实例,需要的朋友可以参考下 推导式是Python中很强大的.很受欢迎的特性,具有语言简洁,速度快等优点.推导式包括: 1.列表推导式 2.字典推导式 3.集合推导式 嵌套列表推导式 NOTE: 字典和集合推导是最近才加入到Python的(Python 2.7 和Python 3.1以上版). 下面简要介绍下: [列表推导式] 列表推导能非常简洁的构造一个新列表:只用一条简洁的表达式即可对得到

Python中tell()方法的使用详解

  这篇文章主要介绍了Python中tell()方法的使用详解,是Python入门学习中的基础知识,需要的朋友可以参考下 tell()方法返回的文件内的文件读/写指针的当前位置. 语法 以下是tell()方法的语法: ? 1 fileObject.tell() 参数 NA 返回值 此方法返回该文件中读出的文件/写指针的当前位置. 例子 下面的例子显示了tell()方法的使用. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #

Python中的rfind()方法使用详解

  这篇文章主要介绍了Python中的rfind()方法使用详解,是Python入门中的基础知识,需要的朋友可以参考下 rfind()方法返回所在子str 被找到的最后一个索引,或者-1,如果没有这样的索引不存在,可选择限制搜索字符串string[beg:end]. 语法 以下是rfind()方法的语法: ? 1 str.rfind(str, beg=0 end=len(string)) 参数 str -- 此选项指定要搜索的字符串 beg -- 这是开始索引,默认情况下为 0 end -- 这

Python中的rjust()方法使用详解

  这篇文章主要介绍了Python中的rjust()方法使用详解,是Python学习入门中的基础知识,需要的朋友可以参考下 rjust()该方法返回字符串合理字符串的右边的长度宽度.填充是通过使用指定的fillchar(默认为空格).如果宽度小于len(s)返回原始字符串. 语法 以下是rjust()方法的语法: ? 1 str.rjust(width[, fillchar]) 参数 width -- 这是字符串填充后总共的长度. fillchar -- 这是填充字符,默认为空格. 返回值 此方

Python中运算符"=="和"is"的详解_python

前言 在讲is和==这两种运算符区别之前,首先要知道Python中对象包含的三个基本要素,分别是:id(身份标识).python type()(数据类型)和value(值).is和==都是对对象进行比较判断作用的,但对对象比较判断的内容并不相同.下面来看看具体区别在哪. Python中比较两个对象是否相等,一共有两种方法,简单来说,它们的区别如下:      is是比较两个引用是否指向了同一个对象(引用比较).      ==是比较两个对象是否相等. >>> a = [1, 2, 3]