Here's a look at a lot of common document formats for software:
[DEFAULT] Serveraliveinterval = 45Compression = Yescompressionlevel = 9forwardx11 = Yes [bitbucket.org]user = HG [ Topsecret.server.com]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[' Fo RwardX11 '] = ' no ' # same hereconfig[' DEFAULT ' [' ForwardX11 '] = ' yes ' <br>with open (' Example.ini ', ' W ') as ConfigFile : Config.write (ConfigFile)
Import Configparserconfig = Configparser. Configparser () #---------------------------------------------Check print (config.sections ()) #[]config.read (' Example.ini ') print (Config.sections ()) #[' bitbucket.org ', ' topsecret.server.com ']print (' bytebong.com ' in config) # Falseprint (config[' bitbucket.org ' [' User ']) # hgprint (config[' DEFAULT ' [' Compression ']) #yesprint (config[') Topsecret.server.com ' [' ForwardX11 ']) #nofor key in config[' bitbucket.org ']: print (key) # user# serveraliveinterval# Co mpression# compressionlevel# forwardx11print (config.options (' bitbucket.org ')) #[' user ', ' serveraliveinterval ', ' Compression ', ' compressionlevel ', ' forwardx11 ']print (config.items (' bitbucket.org ')) #[(' Serveraliveinterval ', ' 45 ') ), (' Compression ', ' yes '), (' CompressionLevel ', ' 9 '), (' forwardx11 ', ' yes '), (' User ', ' HG ')]print (Config.get (' Bitbucket.org ', ' compression ')) #yes #---------------------------------------------Delete, change, add (Config.write (' I.cfg ("W"))) config.add_section (' Yuan ') config.remove_section (' TopsecreT.server.com ') config.remove_option (' bitbucket.org ', ' user ') Config.set (' bitbucket.org ', ' K1 ', ' 11111 ') config.write (Open (' I.cfg ', "w"))
Python configparser module