Reading the ini configuration file in Python

Source: Internet
Author: User
> Py provides ini configuration file operations

The configuration file is an XML file. You still like the use of XML files. But sometimes it's just a simpleProgramTo implement a simple name: value relationship. It is unnecessary to use an XML file. This requirement complies with the INI File Format of Ms. So here we will mainly introduce how to operate the INI file, and the first py application recently written also uses the ini
What is an INI file?
The INI file supported by Py is different from that defined in Windows. It not only supports name = value, but also supports name: value.

> Py supports reading ini configuration files

The configparser module of Py defines three classes to operate on the INI file. Rawconfigparser, configparser, and safeconfigparser
Rawcnfigparser is the most basic INI File Reading Class.
Configparser and safeconfigparser support the $ (value) s variable.

> Rawconfigparser class usage

Int File

[ Weburl ]
Urlname = Http://pumaboyd.cnblogs.com

Test. py file

Import Configparser, OS
From   _ Future __   Import With_statement
Cfg = Configparser. rawconfigparser ()
With open ( " App. ini " ) As fobj
Cfg. readfp (fobj)
Print Cfg. Get ( " Weburl " , " Urlname " )

 

> Configparser class usage

The configration class is extended from rawconfigparser and supports the $ () s variable.
Extended get () and items () of rawconfigparserd

 

Int File

[ Default ]
Val = Pumabyd
[ Weburl ]
Name = % (VAL) S

 

Test. py file

Import Configparser, OS
From   _ Future __   Import With_statement
Cfg = Configparser. configparser ()
With open ( " App. ini " ) As fobj
Cfg. readfp (fobj)
Print Cfg. defaults ()
Print Cfg. Get ( " Weburl " , " Name " )

 

You can see the pumaboyd input by CFG. Get ("weburl", "name. If rawconfigparser is used here, you will see that the output is % (VAL) s.
Note that the default node is default. It can only be read through cfg. defaults. Cfg. Sections () does not contain the default node.

> How to Use the safeconfigparser class

Inherited from configparser. In fact, rawconfigparser is extended and $ () s variable is supported.

Int File

[ Default ]
Val = Pumabyd
[ Weburl ]
Name = ABCD

 

Test. py file

Import Configparser, OS
From   _ Future __   Import With_statement
Cfg = Configparser. saftconfigparser ()
With open ( " App. ini " ) As fobj
Cfg. readfp (fobj)
Cfg. Set ( " Weburl " , " Name " , " & (VAL) S " )
Print Cfg. Get ( " Weburl " , " Name " )

The input result is pumaboyd. If rawconfigparser is used, you can see that the output is % () s.

> How to modify an INI File

The methods in rawconfigparser, safeconfigparser, and configparser are only modifications to the configparser object and are not actually saved to the INI file. Therefore, you need to write the modification back to the INI file by using the write method (this method is available in all three classes.

INI File

 

[ Weburl ]
Name = ABCD

 

Test. py file

Import Configparser, OS
From   _ Future __   Import With_statement
Cfg = Configparser. configparser ()
With open ( " App. ini " ) As fobj
Cfg. readfp (fobj)
Cfg. Set ( " Weburl " , " Name " , " Pumabyd " )

With open ("App. ini","W") As fwobj
Cfg. Write (fwobj)

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.