Python parsing ini, conf, cfg files

Source: Internet
Author: User

1. Use the Configparser module that comes with Python:

#test. cfg file contents: [sec_a]a_key1 = 20a_key2 = 10[sec_b]b_key1 = 121b_key2 = B_value2b_key3 = $rb _key4 = 127.0.0.1
# -* - coding: utf-8 -* -import configparser# generating the Config object conf =  Configparser.configparser () #用config对象读取配置文件conf. Read ("Test.cfg") #以列表形式返回所有的sectionsections  =  Conf.sections () print  ' sections: ',  sections         # sections: [' sec_b ',  ' sec_a '] #得到指定section的所有optionoptions  = 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 ',  '),  (' A_key2 ',  ')] #指定section, option Read value 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: 20print  "Value for sec_a ' s  a_key2: ", int_val    #value  for sec_a ' s a_key2: 10# write config file # Updates the value of the specified section,option conf.set ("Sec_b",  "B_key3",  "new-$r") #写入指定section增加新option和值conf. Set ("Sec_b",   "B_newkey",  "New-value") #增加新的sectionconf. Add_section (' a_new_section ') conf.set (' a_new_section '),   ' New_key ',  ' New_value ') #写回配置文件conf. Write (Open ("Test.cfg",  "W"))

2.C some questions about Onfigparser:

1, cannot be case sensitive.
2, the re-write INI file cannot retain the comments of the original INI file.
3, the re-write INI file cannot be kept in its original order.
4, nesting is not supported.
5, format verification is not supported.

Try the following:configobj module

#读文件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  Configobjconfig = configobj () config.filename = filename#config[' keyword1 '] = value1config[' keyword2 '] = value2#config[ ' Section1 '] = {}config[' section1 ' [' Keyword3 '] = value3config[' 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 ()


Python parsing ini, conf, cfg files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.