Pyyaml Module
Python can also easily handle the Ymal document format, but requires the installation of a module, reference document: Http://pyyaml.org/wiki/PyYAMLDocumentation
Configparser module of common modules
Used to generate and modify common configuration documents, 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 document formats for software
[DEFAULT] Serveraliveinterval = 45Compression = Yescompressionlevel = 9forwardx11 = yes [Bitbucket.org]user = HG [topsecret.server.c Om]port = 50022forwardx11 = no
What if you want to use Python to generate a document like this?
Import Configparser config = Configparser. Configparser () config["DEFAULT"] = {' Serveraliveinterval ': ' A ', ' Compression ': ' Yes ', ' compressionlevel ': ' 9 '} config[' bitbucket.org ' = {}config[' bitbucket.org ' [' User '] = ' HG ' config[' topsecret.server.com '] = {}topsecret = config[' topsecret.server.com ']topsecret[' Host Port ' = ' 50022 ' # mutates the parsertopsecret[' ForwardX11 '] = ' no ' c3/># same hereconfig[' DEFAULT ' [' ForwardX11 '] = ' yes ' with open (' Example.ini ', ' W ') as ConfigFile: config.write ( ConfigFile)
When you're done, you can read it again.
1>>>ImportConfigparser2>>> config =Configparser. Configparser ()3>>>config.sections ()4 []5>>> Config.read ('Example.ini')6['Example.ini']7>>>config.sections ()8['bitbucket.org','topsecret.server.com']9>>>'bitbucket.org' inchConfigTen True One>>>'bytebong.com' inchConfig A False ->>> config['bitbucket.org']['User'] - 'HG' the>>> config['DEFAULT']['Compression'] - 'Yes' ->>> Topsecret = config['topsecret.server.com'] ->>> topsecret['ForwardX11'] + 'No' ->>> topsecret['Port'] + '50022' A>>> forKeyinchconfig['bitbucket.org']:Print(Key) at ... - User - CompressionLevel - Serveraliveinterval - Compression - forwardx11 in>>> config['bitbucket.org']['ForwardX11'] - 'Yes'View Code
Configparser additions and deletions to check grammar
1 [Section1]2K1 =v13 K2:v24 5 [Section2]6K1 =v17 8 ImportConfigparser9 TenConfig =Configparser.configparser () OneConfig.read ('i.cfg') A - ########### Read ########## - #secs = config.sections () the #print secs - #options = config.options (' group2 ') - #Print Options - + #item_list = Config.items (' group2 ') - #Print Item_list + A #val = config.get (' group1 ', ' key ') at #val = config.getint (' group1 ', ' key ') - - ########### rewrite ########## - #sec = config.remove_section (' group1 ') - #config.write (Open (' I.cfg ', "w")) - in #sec = config.has_section (' Wupeiqi ') - #sec = config.add_section (' Wupeiqi ') to #config.write (Open (' I.cfg ', "w")) + - the #config.set (' group2 ', ' K1 ', 11111) * #config.write (Open (' I.cfg ', "w")) $ Panax Notoginseng #config.remove_option (' group2 ', ' age ') - #config.write (Open (' I.cfg ', "w"))View Code
Python Learning Notes-basic "Sixth Week"--pyyaml & Configparser Modules