Use Configparser to parse INI config file instance in Python _python

Source: Internet
Author: User
Tags in python

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

Copy Code code as follows:

[Section1]
Option1:value1
Option2:value2

Python provides a simple module configparser can be used to parse files similar to this form. For the Configparser module, you can parse types such as Key:value and Key=value, and the line for # and, the beginning, is automatically ignored. Corresponds to a comment line. Common functions:
Copy Code code as follows:

Configparser.rawconfigparser ()

The actions of the Rawconfigparser object are:

. Sections (): Returns all available section
. AddSection (SectionName): Adding a section
. Set (SectionName, Optionname, optionvalue): Add option
. Hassection (sectionname): judging
Options (sectionname): Returns the option available under section
. Hasoption (SectionName, optionname): Judge
. read (FileName): Reading files
. Wrie (FileName): Writes a Rawconfigparser object to a file
. Get (SectionName, optionname): Gets the value, by default, returns a String type
GetFloat,. Getint,. Getboolean: Get different types of return values, like parameters
. Items (sectionname): List all Key:value under section
. Remove (sectionname): Delete section
. Remove (SectionName, option_name): Delete an option under section


Demo--Generate files
Copy Code code as follows:

$ cat ini_demo.py
#-*-Coding:utf-8-*-

Import Configparser

Def 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 file generated is:
Copy Code code as follows:

$ cat Test
[Section_a]
OPTION_A1 = APPLE_A1
OPTION_A2 = Banana_a2

[Section_c]
OPTION_C2 = Banana_c2
OPTION_C1 = Apple_c1

[Section_b]
OPTION_B1 = Apple_b1
OPTION_B2 = Banana_b2
Demo--Read files

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 results are:
Copy Code code as follows:

[' section_a ', ' section_c ', ' Section_b ']
[(' Option_a2 ', ' banana_a2 '), (' option_a1 ', ' apple_a1 ')]
Apple_a1

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.