1. 程式人生 > >python-ConfigParser模組【讀寫配置檔案】

python-ConfigParser模組【讀寫配置檔案】

以下的文章就是對python 讀寫配置檔案的具體方案的介紹

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

  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

測試檔案test.py
  1. # -* - coding: UTF-8 -* -

  2. import ConfigParser

  3. #生成config物件

  4. conf = ConfigParser.ConfigParser()

  5. #用config物件讀取配置檔案

  6. conf.read("test.cfg",encoding='utf-8')

  7. #以列表形式返回所有的section

  8. sections = conf.sections()

  9. print 'sections:', sections #sections: ['sec_b', 'sec_a']

  10. #得到指定section的所有option

  11. options = conf.options("sec_a")

  12. print 'options:', options #options: ['a_key1', 'a_key2']

  13. #得到指定section的所有鍵值對

  14. kvs = conf.items("sec_a")

  15. print 'sec_a:', kvs #sec_a: [('a_key1', '20'), ('a_key2', '10')]

  16. #指定section,option讀取值

  17. str_val = conf.get("sec_a", "a_key1")

  18. int_val = conf.getint("sec_a", "a_key2")

  19. print "value for sec_a's a_key1:", str_val #value for sec_a's a_key1: 20

  20. print "value for sec_a's a_key2:", int_val #value for sec_a's a_key2: 10

  21. #寫配置檔案

  22. #更新指定section,option的值

  23. conf.set("sec_b", "b_key3", "new-$r")

  24. #寫入指定section增加新option和值

  25. conf.set("sec_b", "b_newkey", "new-value")

  26. #增加新的section

  27. conf.add_section('a_new_section')

  28. conf.set('a_new_section', 'new_key', 'new_value')

  29. #寫回配置檔案

  30. conf.write(open("test.cfg", "w"))

 

2.2,測試2

配置檔案test.cfg

  1. [info]

  2. age = 21

  3. name = chen

  4. sex = male

測試檔案test.py

  1. from __future__ import with_statement

  2. import ConfigParser

  3. config=ConfigParser.ConfigParser()

  4. with open("test.cfg","rw") as cfgfile:

  5. config.readfp(cfgfile)

  6. name=config.get("info","name")

  7. age=config.get("info","age")

  8. print name

  9. print age

  10. config.set("info","sex","male")

  11. config.set("info","age","55")

  12. age=config.getint("info","age")

  13. print name

  14. print type(age)

  15. 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
  1. [portal]

  2. url = http://%(host)s:%(port)s/Portal

  3. host = localhost

  4. port = 8080

使用RawConfigParser:
  1. import ConfigParser

  2. conf = ConfigParser.RawConfigParser()

  3. print "use RawConfigParser() read"

  4. conf.read("test.cfg")

  5. print conf.get("portal", "url")

  6. print "use RawConfigParser() write"

  7. conf.set("portal", "url2", "%(host)s:%(port)s")

  8. print conf.get("portal", "url2")

得到輸出
  1. use RawConfigParser() read

  2. http://%(host)s:%(port)s/Portal

  3. use RawConfigParser() write

  4. %(host)s:%(port)s

改用ConfigParser
  1. import ConfigParser

  2. conf = ConfigParser.ConfigParser()

  3. print "use RawConfigParser() read"

  4. conf.read("test.cfg")

  5. print conf.get("portal", "url")

  6. print "use RawConfigParser() write"

  7. conf.set("portal", "url2", "%(host)s:%(port)s")

  8. print conf.get("portal", "url2")

得到輸出
  1. use RawConfigParser() read

  2. http://localhost:8080/Portal

  3. use RawConfigParser() write

  4. localhost:8080

改用SafeConfigParser,效果與ConfigParser相同
  1. import ConfigParser

  2. conf = ConfigParser.SafeConfigParser()

  3. print "use RawConfigParser() read"

  4. conf.read("test.cfg")

  5. print conf.get("portal", "url")

  6. print "use RawConfigParser() write"

  7. conf.set("portal", "url2", "%(host)s:%(port)s")

  8. print conf.get("portal", "url2")

 

結論:

還是用ConfigParser