python解析ini、conf、cfg檔案

傻呆發表於2015-08-26
1.使用python自帶的ConfigParser模組:

#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


讀配置檔案   


# -* - 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.ConfigParser的一些問題:

                    1,不能區分大小寫。
                    2,重新寫入的ini檔案不能保留原有 INI檔案的註釋。
                    3,重新寫入的ini檔案不能保持原有的順序。
                    4,不支援巢狀。
                    5,不支援格式校驗。


嘗試下:configobj模組

    
#讀檔案
from configobj import ConfigObj  
    config = ConfigObj(filename)  
    #  
    value1 = config['keyword1']  
    value2 = config['keyword2']  
    #  
    section1 = config['section1']  
    value3 = section1['keyword3']  
    value4 = section1['keyword4']  
    #  
    # you could also write  
    value3 = config['section1']['keyword3']  
    value4 = config['section1']['keyword4']
     
     
#寫檔案如下:
from configobj import ConfigObj
config = ConfigObj()
config.filename = filename
#
config['keyword1'] = value1
config['keyword2'] = value2
#
config['section1'] = {}
config['section1']['keyword3'] = value3
config['section1']['keyword4'] = value4
#
section2 = {
    'keyword5': value5,
    'keyword6': value6,
    'sub-section': {
        'keyword7': value7
        }
}
config['section2'] = section2
#
config['section3'] = {}
config['section3']['keyword 8'] = [value8, value9, value10]
config['section3']['keyword 9'] = [value11, value12, value13]
#
config.write()

相關文章