> 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)