The structure of the Config.ini file is the following: The structure is a section, a part of the structure under the "[]". There are three sections below, each of which is Section0,section1,section2
[MySQL config]Host=127.0.0.1Port=8080username=Rootpassword=123456[Online config]Online=Www.online.comusername=Peixmpassword=123qwe[Test config]Test=Www.test.comusername=Peixmpassword=123qwe
So, how do you get those things in your code?
First, you need to have a config.ini file
#!/usr/bin/env/python#-*-coding:utf-8-*-#Authour:xiapmin_peiImportConfigparser,os#encapsulates a path and directly enters the file name filename to get the path to filenamedefgetpath (filename):returnOs.path.join (Os.path.dirname (__file__), Os.pardir,'Data', filename)classConfig (object):def __init__(self,filename,section):""":p Aram FileName: File name:p Aram section: Part of the file, which is plastic"""self.section= Section#instantiate a Configparser objectSELF.CF =Configparser. Configparser ()#read the contents of a fileself.cf.read (GetPath (filename))defGetConfig (self,avg):"""get the content of the desired attribute:p Aram avg: Property Name: Return: The value of the property""" Printself.cf.sections () parameter=Self.cf.get (Self.cf.sections () [Self.section],avg)returnparameterif __name__=="__main__": #instantiate config, want to config.ini a file, 2nd section contentcon = Config ("Config.ini", 1) #gets the value of the online property PrintCon.getconfig ('Online')
Execution results: Thus, sections is a list
[' MySQL config ', ' online config ', ' Test config ']
Www.online.com
Process finished with exit code 0
Application of Python Config.ini