通过python的ConfigParse模块读写ini配置文件
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                通过python的ConfigParse模块读写ini配置文件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            python讀寫配置文件ConfigParser模塊是python標準庫自帶的讀取配置文件的模塊.通過他可以方便的讀取配置文件.目前示例代碼中的Python版本都是基于2.7版本 
 
官網(wǎng)地址是,異常處理該官網(wǎng)頁面也有介紹
https://docs.python.org/2/library/configparser.html? ??
 
 
配置配置格式 配置文件由section組成,然后其下是name:value或者name=value,注釋項可以使用#或是;
 
 
常用選項: config?=?ConfigParser.ConfigParser()?//直接讀取ini文件內(nèi)容,初始化config實例(建立一個空的數(shù)據(jù)集實例) config.read(filename)??//通過load文件filename來初始化config實例 config.write(open(filename,'w'))???//保存配置
config.get(section,?key)?//獲得指定section中的key的value config.getint(section,?key) config.getfloat(section,?key) config.getboolean(section,?key) config.set(section,?key,?value)??//在指定section中,添加一對key-value鍵值對,沒有section時返回?
下面是我的一個包裝類代碼示例: 配置文件config_demo2.conf, 內(nèi)容如下:
 
 
 
打開配置文件config_demo2.conf, 你會發(fā)現(xiàn)其內(nèi)容已經(jīng)被更新了.
 
參考文獻 http://blog.chinaunix.net/uid-26354188-id-3199820.html? ?#解說非常清晰 http://www.2cto.com/kf/201108/100384.html? 例子也不錯 http://blog.csdn.net/wyzxg/article/details/17263317? ?類包裝,也不錯 http://my.oschina.net/flymaxty/blog/222748? 不過是python 3寫的 http://blog.sina.com.cn/s/blog_3fe961ae0102uwj9.html? 有RawConfigParser類的使用示例 http://www.jb51.net/article/68680.htm? 包裝了一個configurationparser類,有參考價值,但是需要修改,發(fā)現(xiàn)寫得很不規(guī)范。 http://www.linuxidc.com/Linux/2012-02/54759.htm? ?也有一個包裝類
                        
                        
                        https://docs.python.org/2/library/configparser.html? ??
配置配置格式 配置文件由section組成,然后其下是name:value或者name=value,注釋項可以使用#或是;
常用選項: config?=?ConfigParser.ConfigParser()?//直接讀取ini文件內(nèi)容,初始化config實例(建立一個空的數(shù)據(jù)集實例) config.read(filename)??//通過load文件filename來初始化config實例 config.write(open(filename,'w'))???//保存配置
config.get(section,?key)?//獲得指定section中的key的value config.getint(section,?key) config.getfloat(section,?key) config.getboolean(section,?key) config.set(section,?key,?value)??//在指定section中,添加一對key-value鍵值對,沒有section時返回?
下面是我的一個包裝類代碼示例: 配置文件config_demo2.conf, 內(nèi)容如下:
[main]
optdata = eJwzMTAxMODlMgFSpmDKCEqZQSgLMGViyMsFAI+xBn4=
build = 1240
setup = 40400
fileexists = 000000000
qhistorymax = 50
options = 1001000010000010011010011111111000000101111101110000000010000000000100110010011041100000000210010111111113001111010100[LiveUpdate]
interval = 15[QuickConnect]
left = 0
top = 0
width = 424
height = 260
state = 0[Graph]
v = 0
h = 65[window]
left = 124
top = 69
width = 1093
height = 614
state = 0
ts = 0.5
bs = 0.5
cs = 0.75
lts = 0
rts = 0
thp = 546[CmdWindow]
y = 0.629999995231628
w = 454
h = 377[statuswin]
left = 383
top = 154
width = 592
height = 411
state = 0
font = "宋體", 8, [], [clWindowText], 134
color = -2147483643
ontop = 0
wrap = 0
#!/usr/bin/env python
#encoding: utf-8
#description: a wrapper class for ConfigParser module
#date: 2015-10-29import ConfigParserclass cConfParser:def __init__(self, conf_path):self.fpath = conf_path #配置文件路徑,要求是絕對路徑self.cf = ConfigParser.ConfigParser() #ConfigParser對象實例self.cf.read(self.fpath) #一啟動就讀取配置文件def __del__(self):#一關(guān)閉就將ConfigParser對象的內(nèi)容序列化到本地配置文件中with open(self.fpath, 'w') as fh:self.cf.write(fh)fh.close()#添加指定的節(jié)def add_section(self, s):sections = self.cf.sections()if s in sections:returnelse:self.cf.add_section(s)#移除指定的節(jié)def remove_section(self, s):return self.cf.remove_section(s)def get(self, s, o):return self.cf.get(s, o)def set(self, s, o, v):if self.cf.has_section(s):self.cf.set(s, o, v)#移除指定節(jié)內(nèi)的指定選項def remove_option(self, s, o):if self.cf.has_section(s):return self.cf.remove_option(s, o)return False#返回節(jié)內(nèi)的(key, val)列表def items(self, s):return self.cf.items(s)#返回所有節(jié)的列表def sections(self):return self.cf.sections()#返回節(jié)內(nèi)的key列表def options(self, s):return self.cf.options(s)if __name__ == '__main__':config_file = './config_demo2.conf'cp = cConfParser(config_file)print cp.sections()print cp.items('CmdWindow')print cp.options('CmdWindow')print cp.get('main', 'optdata')print cp.get('LiveUpdate', 'Interval')cp.set('LiveUpdate', 'Retry', '3')print cp.remove_option('LiveUpdate', 'Retry')cp.set('LiveUpdate', 'Confirm', 'yes')cp.add_section('Copyright')print cp.remove_section('Copyright')cp.add_section('Copyright2')打開配置文件config_demo2.conf, 你會發(fā)現(xiàn)其內(nèi)容已經(jīng)被更新了.
參考文獻 http://blog.chinaunix.net/uid-26354188-id-3199820.html? ?#解說非常清晰 http://www.2cto.com/kf/201108/100384.html? 例子也不錯 http://blog.csdn.net/wyzxg/article/details/17263317? ?類包裝,也不錯 http://my.oschina.net/flymaxty/blog/222748? 不過是python 3寫的 http://blog.sina.com.cn/s/blog_3fe961ae0102uwj9.html? 有RawConfigParser類的使用示例 http://www.jb51.net/article/68680.htm? 包裝了一個configurationparser類,有參考價值,但是需要修改,發(fā)現(xiàn)寫得很不規(guī)范。 http://www.linuxidc.com/Linux/2012-02/54759.htm? ?也有一個包裝類
總結(jié)
以上是生活随笔為你收集整理的通过python的ConfigParse模块读写ini配置文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 在CentOS 6.3 64bit上使用
- 下一篇: python中的daemon守护进程实现
