Configparser module for operating configuration files
Note: parser translated into "analytic" meaning.
The format of a configuration file is similar to a Windows INI file and can contain one or more sections (section), each of which can have multiple parameters (key = value).
In order to better understand this article, we first understand the configuration file composition and naming: The configuration file (INI file) consists of chapters (section [SectionName]), key, value (Key=value or Key:value).
# create a new config file Testconfig.ini = 50.23.190.57== 3306= TestDB
See the code for specific usage, and write it in detail.
#-*-coding:utf-8-*-#TIME:2017/10/18 22:30#Author:yangyangjun#first import the Configparser moduleImportConfigparser#here it is found that the upper case and the lower lowercase can be, just when the import is how to write, the following instantiation of how to write. #Import ConfigparserConfigFile='Testconfig.ini'#1. Create Configparser instancesConfig=Configparser.configparser ()#2. Read the configuration fileConfig.read (configfile)#3. Get the chapter sequence of the configuration filePrintConfig.sections ()#[' DATABASE ']#4. Get the sequence of all keys in the corresponding section of the configuration filePrintConfig.options ('DATABASE')#[' Host ', ' username ', ' password ', ' Port ', ' database ']#5. Get the value of corresponding key in the corresponding section of the configuration filePrintConfig.get ('DATABASE','Host')#50.23.190.57#6. Get all key-value pairs for the corresponding chapters in the configuration filePrintConfig.items ('DATABASE')#[(' Host ', ' 50.23.190.57 '), (' username ', ' Testyang '), (' Password ', ' Testyang '), (' Port ', ' 3306 '), (' Database ', ' TestDB ')]#7. Add section sections to the configuration file#re-instantiate an object, if you use the object already read above, write will read the contents of the written again. Writeconfig =Configparser.configparser ()#8. Add SectionWriteconfig.add_section ("UserInfo")#9, set to the [UserInfo] node in the configuration file to join the key value pairWriteconfig.set ("UserInfo",'username','Testyang') Writeconfig.set ("UserInfo",'Password','Testyangpassword') Writeconfig.set ("UserInfo",' Age',' -') Writeconfig.set ("UserInfo",'Country',' China')#10. An appended way to open a file that already exists and create the file if it does not existwriteconfig.write (Open ('Testconfig.ini','a'))#The write code above is equivalent to the following code" "f = open (' Testconfig.ini ', ' a ') Writeconfig.write (f)" "#Note If you write Writeconfig.add_section ("UserInfo") separately and then execute the set separately, the following error is reported#so add_section and set are written together, executed together." "raise Nosectionerror (section) ConfigParser.NoSectionError:No section: ' UserInfo '" "#the results after writing are as follows" "[userinfo]username = Testyangpassword = Testyangpasswordage = 18country = China" "
The following results are performed:
# create a new config file Testconfig.ini = 50.23.190.57== 3306=== = =
The application examples are as follows:
#Application example, you can create a new py file to encapsulate the method usedclassreadconfig ():def __init__(self): self.cf=Configparser.configparser () self.cf.read (configfile)defget_userinfo (self, username): Value= Self.cf.get ("UserInfo", username)returnvaluedefget_http (self, name): Value= Self.cf.get ("HTTP", name)returnvaluedefget_db (self, name): Value= Self.cf.get ("DATABASE", name)returnvalueif __name__=="__main__": GetData=Readconfig ()PrintGetdata.get_userinfo ("username")#Testyang Printgetdata.get_db ("Database")#TestDB
Python-configparser Module Learning