標籤:輸出 寫入 config 測試檔案 section host 軟體測試 options tle
1,函數介紹
1.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類型
1.2.寫入設定檔
-add_section(section) 添加一個新的section
-set( section, option, value) 對section中的option進行設定
需要調用write將內容寫入設定檔。
2,測試執行個體2.1,測試1
設定檔test.cfg
[plain] view plain copy
- [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
測試檔案test.py
[python] view plain copy
- # -* - coding: UTF-8 -* -
- import ConfigParser
- #產生config對象
- conf = ConfigParser.ConfigParser()
- #用config對象讀取設定檔
- 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的所有索引值對
- 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,測試2
設定檔test.cfg
[plain] view plain copy
- [info]
- age = 21
- name = chen
- sex = male
測試檔案test.py
[python] view plain copy
- 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 name
- print age
- config.set("info","sex","male")
- config.set("info","age","55")
- age=config.getint("info","age")
- print name
- print type(age)
- print age
分析
其中[ ] 中的info是這段配置的名字。
其中age,name都是屬性。
首先,config=ConfigParser.ConfigParser() 得到一個配置config對象.下面開啟一個設定檔 cfgfile. 用readfp()讀取這個檔案.這樣配置的內容就讀到config對象裡面了。
接下來一個問題是如何讀取值.常用的方法是get() 和getint() . get()返迴文本. getint()返回整數。
其次,name=config.get(‘‘info‘‘,‘‘name‘‘) 意思就是.讀取config中info段中的name變數值。
最後講講如何設定值.使用set(段名,變數名,值) 來設定變數.config.set(‘‘info‘‘,‘‘age‘‘,‘‘21‘‘) 表示把info段中age變數設定為21。
2.3,測試3
python的ConfigParser Module中定義了3個類對INI檔案進行操作。
分別是RawConfigParser、ConfigParser、SafeConfigParser。
RawCnfigParser是最基礎的INI檔案讀取類,ConfigParser、SafeConfigParser支援對%(value)s變數的解析。
設定檔test.cfg
[plain] view plain copy
- [portal]
- url = http://%(host)s:%(port)s/Portal
- host = localhost
- port = 8080
使用RawConfigParser:
[python] view plain copy
- import ConfigParser
-
- conf = 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")
得到輸出
[plain] view plain copy
- use RawConfigParser() read
- http://%(host)s:%(port)s/Portal
- use RawConfigParser() write
- %(host)s:%(port)s
改用ConfigParser
[python] view plain copy
- import ConfigParser
-
- conf = 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")
得到輸出
[plain] view plain copy
- use RawConfigParser() read
- http://localhost:8080/Portal
- use RawConfigParser() write
- localhost:8080
改用SafeConfigParser,效果與ConfigParser相同
[python] view plain copy
- import ConfigParser
-
- conf = 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")
結論:
還是用ConfigParser
python-ConfigParser模組【讀寫設定檔】