Python parsing ini file by configparser

Source: Internet
Author: User

Python parsing ini file by configparser

INI file is a frequently used configuration file in Windows, the main format is:

[Section1]option1 : value1option2 : value2

Python provides a simple module that ConfigParser can be used to parse files similar to this form. For a ConfigParser module to be resolvable key:value and key=value such a type, the # row for and ; beginning will be ignored automatically. Equivalent to the comment line. Common functions:

Configparser.rawconfigparser ()

The operations of the Rawconfigparser object are:

. Sections () : Returns all the available sections
. Add section(section name) : Adding section
. Set (sectionname, optionname, optionvalue): Add option
. Has sections(section name) : Judging
. Options (sectionname) : Returns the option available under section
. Haveoption (sectionname, optionname) : Judging
. Read (filename) : Read files
. Wrie (filename) : Writes the Rawconfigparser object to a file
. Get (SectionName, optionname) : Gets the value, the default is the return string type
. GetFloat,. Getint,. Getboolean : Get different types of return values, parameters like get parameters
. Items (sectionname) : Lists all Key:value under section
. Remove (sectionname) : Delete section
. Remove (sectionname, option_name) : Delete an option under section

Demo--Generate files

$ cat ini_demo.py# -*- coding:utf-8 -*-import ConfigParserdef gen_ini():    ftest = open(‘test‘,‘w‘)    config_write = ConfigParser.RawConfigParser()    config_write.add_section(‘Section_a‘)    config_write.add_section(‘Section_b‘)    config_write.add_section(‘Section_c‘)    config_write.set(‘Section_a‘,‘option_a1‘,‘apple_a1‘)    config_write.set(‘Section_a‘,‘option_a2‘,‘banana_a2‘)    config_write.set(‘Section_b‘,‘option_b1‘,‘apple_b1‘)    config_write.set(‘Section_b‘,‘option_b2‘,‘banana_b2‘)    config_write.set(‘Section_c‘,‘option_c1‘,‘apple_c1‘)    config_write.set(‘Section_c‘,‘option_c2‘,‘banana_c2‘)       config_write.write(ftest)    ftest.close()if __name__ == "__main__":    gen_ini()

The last generated file is:

$ cat test[Section_a]option_a1 = apple_a1option_a2 = banana_a2[Section_c]option_c2 = banana_c2option_c1 = apple_c1[Section_b]option_b1 = apple_b1option_b2 = banana_b2

Demo--Read file

def read_ini():    config_read = ConfigParser.RawConfigParser()    config_read.read(‘test‘)    print config_read.sections()    print config_read.items(‘Section_a‘)    print config_read.get(‘Section_a‘,‘option_a1‘)

The final result is:

[‘Section_a‘, ‘Section_c‘, ‘Section_b‘][(‘option_a2‘, ‘banana_a2‘), (‘option_a1‘, ‘apple_a1‘)]apple_a1

Python parsing ini file by Configparser

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.