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

  1. [sec_a]
  2. a_key1 = 20
  3. a_key2 = 10
  4. [sec_b]
  5. b_key1 = 121
  6. b_key2 = b_value2
  7. b_key3 = $r
  8. b_key4 = 127.0.0.1

parse_test_conf.pylinux

  1. import ConfigParser
  2. cf = ConfigParser.ConfigParser()
  3. #read config
  4. cf.read("test.conf")
  5. # return all section
  6. secs = cf.sections()
  7. print 'sections:', secs
  8. opts = cf.options("sec_a")
  9. print 'options:', opts
  10. kvs = cf.items("sec_a")
  11. print 'sec_a:', kvs
  12. #read by type
  13. str_val = cf.get("sec_a", "a_key1")
  14. int_val = cf.getint("sec_a", "a_key2")
  15. print "value for sec_a's a_key1:", str_val
  16. print "value for sec_a's a_key2:", int_val
  17. #write config
  18. #update value
  19. cf.set("sec_b", "b_key3", "new-$r")
  20. #set a new value
  21. cf.set("sec_b", "b_newkey", "new-value")
  22. #create a new section
  23. cf.add_section('a_new_section')
  24. cf.set('a_new_section', 'new_key', 'new_value')
  25. #write back to configure file
  26. cf.write(open("test.conf", "w"))


獲得終端輸出:
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. [sec_b]
  2. b_newkey = new-value
  3. b_key4 = 127.0.0.1
  4. b_key1 = 121
  5. b_key2 = b_value2
  6. b_key3 = new-$r
  7. [sec_a]
  8. a_key1 = i'm value
  9. a_key2 = 22
  10. [a_new_section]
  11. new_key = new_value


4.Python的ConfigParser Module中定義了3個類對INI文件進行操做。分別是RawConfigParser、ConfigParser、SafeConfigParser。RawCnfigParser是最基礎的INI文件讀取類,ConfigParser、SafeConfigParser支持對%(value)s變量的解析。

設定配置文件test2.confurl

  1. [portal]
  2. url = http://%(host)s:%(port)s/Portal
  3. host = localhost
  4. port = 8080


使用RawConfigParser:spa

  1. import ConfigParser
  2. cf = ConfigParser.RawConfigParser()
  3. print "use RawConfigParser() read"
  4. cf.read("test2.conf")
  5. print cf.get("portal", "url")
  6. print "use RawConfigParser() write"
  7. cf.set("portal", "url2", "%(host)s:%(port)s")
  8. print cf.get("portal", "url2")


獲得終端輸出:
use RawConfigParser() read
http://%(host)s:%(port)s/Portal
use RawConfigParser() write
%(host)s:%(port)sorm

改用ConfigParser:htm

  1. import ConfigParser
  2. cf = ConfigParser.ConfigParser()
  3. print "use ConfigParser() read"
  4. cf.read("test2.conf")
  5. print cf.get("portal", "url")
  6. print "use ConfigParser() write"
  7. cf.set("portal", "url2", "%(host)s:%(port)s")
  8. print cf.get("portal", "url2")


獲得終端輸出:
use ConfigParser() read
http://localhost:8080/Portal
use ConfigParser() write
localhost:8080get

改用SafeConfigParser:it

  1. import ConfigParser
  2. cf = ConfigParser.SafeConfigParser()
  3. print "use SafeConfigParser() read"
  4. cf.read("test2.conf")
  5. print cf.get("portal", "url")
  6. print "use SateConfigParser() write"
  7. cf.set("portal", "url2", "%(host)s:%(port)s")
  8. print cf.get("portal", "url2")


獲得終端輸出(效果同ConfigParser):
use SafeConfigParser() read
http://localhost:8080/Portal
use SateConfigParser() write
localhost:8080 io

相關文章
相關標籤/搜索