Overview
INI is one of the configuration file formats that we often see.
INI is a file name extension (also commonly used in other systems) in the Microsoft Windows operating system.
INI "initialization (Initial)" is abbreviated. As the term indicates, the INI file is used to initialize or set parameters on the operating system or a specific program.
Its basic form is as follows:
= == = = = = = = Value4
We use the Python configparser module to read and write INI files.
Configparser Read
Read (filename) reads INI file contents
Sections () Gets all the sections and returns as a list
Options (sections) gets all option for the specified section
Get (section,option) Gets the value of option in section, returned as a string type
Write
Instance
Our husband becomes an INI file and writes the data and then reads it out to show.
#-*-coding:utf-8-*-__author__='Gubei'#Import ModuleImportConfigparserif __name__=="__main__": #build an object firstConfig =Configparser. Configparser ()#to get us to write a few sets of data #first add a sectionConfig.add_section ("Open Source Optimization test") #Add key-value Key-value pairs under the new sectionConfig.set ("Blog Park","Cnblog","Gubei") Config.set ("Cnblog","Gubei","Python3") Config.set ("Gubei","Pyhon3","INI file read") #add another section, but no Key-value key value pairConfig.add_section ("the night has been so deep") #Write FileWith open ('Iniconfig.ini','W') as Configfile:config.write (configfile)##################################### #let's start by reading the INI file just now .Config.read ("Iniconfig.ini") #get all of its sectionsSections =config.sections ()Print(sections)#get all the options under section forSecinchsections:options=config.options (sec)Print(Options)#get the corresponding value value according to sections and options forSecinchsections: forOptioninchconfig.options (sec):Print("[%s]%s=%s"% (sec, option, Config.get (sec, option)))
Summary
The INI file scenario is typically used to initialize the configuration file, but you can also use it for data storage if you wish.
To keep a small problem, try encapsulating a generic INI file operation class with your class.
[Python3] INI file read and write