python-ConfigParser模块【读写配置文件】
對(duì)python 讀寫配置文件的具體方案的介紹
1,函數(shù)介紹
import configParser 如果Configparser無(wú)效將導(dǎo)入的configParser 的C小寫1.1.讀取配置文件
-read(filename)?直接讀取ini文件內(nèi)容
-sections()?得到所有的section,并以列表的形式返回
-options(section)?得到該section的所有option
-items(section)?得到該section的所有鍵值對(duì)
-get(section,option)?得到section中option的值,返回為string類型
-getint(section,option)?得到section中option的值,返回為int類型
?
1.2.寫入配置文件
?
-add_section(section)?添加一個(gè)新的section
-set( section, option, value) 對(duì)section中的option進(jìn)行設(shè)置
? 需要調(diào)用write將內(nèi)容寫入配置文件。
?
2,測(cè)試實(shí)例
?2.1,測(cè)試1
? ?配置文件test.cfg
[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
?測(cè)試文件test.py
# -* - coding: UTF-8 -* - import configParser #生成config對(duì)象 conf = configParser.ConfigParser() #用config對(duì)象讀取配置文件 conf.read("test.cfg") #以列表形式返回所有的section sections = conf.sections() print 'sections:', sections #sections: ['sec_b', 'sec_a'] #得到指定section的所有option options = conf.options("sec_a") print 'options:', options #options: ['a_key1', 'a_key2'] #得到指定section的所有鍵值對(duì) kvs = conf.items("sec_a") print 'sec_a:', kvs #sec_a: [('a_key1', '20'), ('a_key2', '10')] #指定section,option讀取值 str_val = conf.get("sec_a", "a_key1") int_val = conf.getint("sec_a", "a_key2")print "value for sec_a's a_key1:", str_val #value for sec_a's a_key1: 20 print "value for sec_a's a_key2:", int_val #value for sec_a's a_key2: 10#寫配置文件 #更新指定section,option的值 conf.set("sec_b", "b_key3", "new-$r") #寫入指定section增加新option和值 conf.set("sec_b", "b_newkey", "new-value") #增加新的section conf.add_section('a_new_section') conf.set('a_new_section', 'new_key', 'new_value') #寫回配置文件 conf.write(open("test.cfg", "w"))
2.2,測(cè)試2
配置文件test.cfg [info] age = 21 name = chen sex = male
測(cè)試文件test.py
from __future__ import with_statement import configParser config=ConfigParser.ConfigParser() with open("test.cfg","rw") as cfgfile:config.readfp(cfgfile)name=config.get("info","name")age=config.get("info","age")print nameprint ageconfig.set("info","sex","male")config.set("info","age","55")age=config.getint("info","age")print nameprint type(age)print age?
分析
其中[ ] 中的info是這段配置的名字。
其中age,name都是屬性。
首先,config=ConfigParser.ConfigParser()?得到一個(gè)配置config對(duì)象.下面打開一個(gè)配置文件 cfgfile. 用readfp()讀取這個(gè)文件.這樣配置的內(nèi)容就讀到config對(duì)象里面了。
接下來(lái)一個(gè)問(wèn)題是如何讀取值.常用的方法是get() 和getint() . get()返回文本. getint()返回整數(shù)。
其次,name=config.get(''info'',''name'') ?意思就是.讀取config中info段中的name變量值。
最后講講如何設(shè)置值.使用set(段名,變量名,值) 來(lái)設(shè)置變量.config.set(''info'',''age'',''21'') 表示把info段中age變量設(shè)置為21。
?
2.3,測(cè)試3
Python的ConfigParser Module中定義了3個(gè)類對(duì)INI文件進(jìn)行操作。
分別是RawConfigParser、ConfigParser、SafeConfigParser。
RawCnfigParser是最基礎(chǔ)的INI文件讀取類,ConfigParser、SafeConfigParser支持對(duì)%(value)s變量的解析。?
配置文件test.cfg
[portal] url = http://%(host)s:%(port)s/Portal host = localhost port = 8080使用RawConfigParser:
import ConfigParserconf = ConfigParser.RawConfigParser() print "use RawConfigParser() read" conf.read("test.cfg") print conf.get("portal", "url")print "use RawConfigParser() write" conf.set("portal", "url2", "%(host)s:%(port)s") print conf.get("portal", "url2")得到輸出
use RawConfigParser() read http://%(host)s:%(port)s/Portal use RawConfigParser() write %(host)s:%(port)s改用ConfigParser
import ConfigParserconf = ConfigParser.ConfigParser() print "use RawConfigParser() read" conf.read("test.cfg") print conf.get("portal", "url")print "use RawConfigParser() write" conf.set("portal", "url2", "%(host)s:%(port)s") print conf.get("portal", "url2")得到輸出
use RawConfigParser() read http://localhost:8080/Portal use RawConfigParser() write localhost:8080改用SafeConfigParser,效果與ConfigParser相同
import ConfigParserconf = ConfigParser.SafeConfigParser() print "use RawConfigParser() read" conf.read("test.cfg") print conf.get("portal", "url")print "use RawConfigParser() write" conf.set("portal", "url2", "%(host)s:%(port)s") print conf.get("portal", "url2")
結(jié)論:
還是用ConfigParser
?
以前不會(huì)排版,最近才學(xué)會(huì)排版,看著舒服多了 -------------------------------------------------------------------------------------------------------- 來(lái)源:http://blog.csdn.net/gexiaobaohelloworld/article/details/7976944?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的python-ConfigParser模块【读写配置文件】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 正则表达式 分组
- 下一篇: MySQL(十一)之触发器