功能介紹:
在程式中使用配置檔案來靈活的配置一些引數是一件很常見的事情,配置檔案的解析並不複雜,在python裡更是如此,在官方發布的庫中就包含有做這件事情的庫,那就是configparser,這裡簡單的做一些介紹。
configparser解析的配置檔案的格式比較象ini的配置檔案格式,就是檔案中由多個section構成,每個section下又有多個配置項。
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
parser_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_a', 'sec_b']
options: ['a_key1', 'a_key2']
sec_a: [('a_key1', '20'), ('a_key2', '10')]
value for sec_a's a_key1: 20
value for sec_a's a_key2: 10
4.python的configparser module中定義了3個類對ini檔案進行操作。分別是rawconfigparser、configparser、safeconfigparser。rawcnfigparser是最基礎的ini檔案讀取類,configparser、safeconfigparser支援對%(value)s變數的解析。
設定配置檔案test2.conf
使用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")
得到終端輸出:
改用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")
得到終端輸出:
改用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):
python ConfigParser模組介紹
python讀配置檔案的包 configparser configparser模組在python中用來讀取配置檔案,配置檔案的格式跟windows下的ini配置檔案相似,可以包含乙個或多個節 section 每個節可以有多個引數 鍵 值 使用的配置檔案的好處就是不用在程式設計師寫死,可以使程式更靈活...
python configparser模組用法
配置檔案的資訊 mysqld charater server set utf 8 default engine innodb skip grant table true port 3306 client user root password 123 egon name egon age 18conf...
Python ConfigParser 注意事項
以這個非常簡單的典型配置檔案為例 default serveraliveinterval 45 compression yes compressionlevel 9 forwardx11 yes bitbucket.org user hg topsecret.server.com port 5002...