This module is used to generate and modify common configuration documents, and the name of the current module is changed to Configparser in the Python 3.x version.
Here's a look at a lot of the common configuration file formats for software
[DEFAULT]ServerAliveInterval = 45Compression = yesCompressionLevel = 9ForwardX11 = yes[bitbucket.org]User = hg[topsecret.server.com]Port = 50022ForwardX11 = no
Parsing the configuration file
>>>Import Configparser>>> Config= Configparser. Configparser()>>> Config. Sections()[]>>> Config. read(' Example.ini ')[' Example.ini ']>>> Config. Sections()[' Bitbucket.org ',' Topsecret.server.com ']>>>' Bitbucket.org 'in configTrue>>>' Bytebong.com 'in configFalse>>> Config[' Bitbucket.org '][' User ']' HG '>>> Config[' DEFAULT '][' Compression ']' Yes '>>> Topsecret= Config[' Topsecret.server.com ']>>> Topsecret[' ForwardX11 ']' No '>>> Topsecret[' Port ']' 50022 '>>> for key in config[ ' bitbucket.org ' ]print (Key" ..usercompressionlevelserveraliveintervalcompressionforwardx11>>> config[ ' bitbucket.org ' ][< Span class= "token string" > ' ForwardX11 ' ] ' yes '
Other additions and deletions to check grammar
[group1]k1 = v1k2:v2[group2]k1 = v1import ConfigParserconfig = ConfigParser.ConfigParser()config.read(‘i.cfg‘)# ########## 读 ###########secs = config.sections()#print secs#options = config.options(‘group2‘)#print options#item_list = config.items(‘group2‘)#print item_list#val = config.get(‘group1‘,‘key‘)#val = config.getint(‘group1‘,‘key‘)
# ########## 改写 ##########
Import Configparser
Config = Configparser. Configparser ()
Config.read (' Test.ini ')
# sec = config.remove_section (' group1 ')
# config.write (Open (' I.cfg ', ' W '))
# sec = config.has_section (' Wupeiqi ')
# Print (SEC)
# sec = config.add_section (' Wupeiqi ')
# config.write (Open (' I.cfg ', ' W '))
# config.set (' group2 ', ' K1 ', ' 111111 ')
# config.write (Open (' I.cfg ', ' W '))
Config.remove_option (' group2 ', ' K3 ')
Config.write (Open (' I.cfg ', ' W '))
Python Basic-configparser module