Python-ConfigParser常用操作樣本__Python

來源:互聯網
上載者:User

configParser 模組用於操作設定檔
設定檔的格式和windows的ini檔案類似,可以包含一個或者多個節(section),每個節又可以包含多個參數(索引值對形式: key=value) ConfigParser 常用方法

1、config=ConfigParser.ConfigParser()  建立ConfigParser執行個體  2、config.sections()  --> list返回設定檔中所有節的序列3、config.options(section)  --> list返回某個項目中的所有鍵的序列  4、config.get(section,option)  --> str返回section節中,option的索引值  5、config.add_section(str)  添加一個設定檔節(str)  6、config.set(section,option,val)  設定section節中,鍵名為option的值(val)  7、config.read(filename)  讀取設定檔  8、config.write(obj_file)  寫入設定檔  
綜合樣本
#!/usr/bin/env python# -*- coding: utf-8 -*-import ConfigParserdef writeConfig(filename):    '''    寫設定檔    設定檔全路徑名:param filename:    '''    config = ConfigParser.ConfigParser()    section_name = "section_1"    config.add_section(section_name)    config.set(section_name, "key_1", "value_1_1")    config.set(section_name, "key_2", "value_1_2")    config.set(section_name, "key_3", "value_1_3")    section_name = "section_2"    config.add_section(section_name)    config.set(section_name, "key_1", "value_2_1")    config.set(section_name, "key_2", "value_2_2")    config.set(section_name, "key_3", "value_2_3")    config.write(open(filename, "w"))def updateConfig(filename, section, **keyv):    '''    修改設定檔    檔案名稱:param filename:    section節:param section:    配置索引值對(key=value):param keyv:    :return:    '''    config = ConfigParser.ConfigParser()    config.read(filename)    for key in keyv:        config.set(section, key, keyv[key])    config.write(open(filename, "w"))def printConfig(filename):    '''    列印設定檔資訊    設定檔全路徑名:param filename:    '''    config = ConfigParser.ConfigParser()    config.read(filename)    sections = config.sections()    print "sections:", sections    for section in sections:        print "[%s]" % section        for option in config.options(section):            print "\t%s=%s" % (option, config.get(section, option))if __name__ == '__main__':    file_name = 'test.ini'    writeConfig(file_name)    printConfig(file_name)    updateConfig(file_name, 'section_2', key_2='修改了')    print "修改後:"    printConfig(file_name)    print "end"
執行結果
sections: ['section_1', 'section_2'][section_1]    key_1=value_1_1    key_2=value_1_2    key_3=value_1_3[section_2]    key_1=value_2_1    key_2=value_2_2    key_3=value_2_3修改後:sections: ['section_1', 'section_2'][section_1]    key_1=value_1_1    key_2=value_1_2    key_3=value_1_3[section_2]    key_1=value_2_1    key_2=修改了    key_3=value_2_3end
test.ini檔案
[section_1]key_1 = value_1_1key_2 = value_1_2key_3 = value_1_3[section_2]key_1 = value_2_1key_2 = 修改了key_3 = value_2_3
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.