The Configparser is a Python-brought module used to read and write configuration files.
A specific format:
1 # Note 1 2 ; Note 23 [Node1] # node 4 key1=value1 # key value 5 ... 6 7 [Node2] ; node 8 key2=value2; node 9 ...
format
Usage:
1 ImportConfigparser2 3 #Create a Configparser object4config=Configparser. Configparser ()5 #loading a configuration file of a specific format into memory6Config.read ("Node.txt", encoding="Utf-8")7 #or all of the nodes8Node=config.sections ()9 Print(node)Ten One #gets the specified key AAllkey=config.options ("Node1") - Print(Allkey) - the #gets the value of the key below the specified node -Key=config.get ("Node2","Key2") - Print(Key) - + #Adding nodes -Config.add_section ("Node3") + A #Delete a node atConfig.remove_section ("Node1") - - #Check if there are any nodes -Hasnode=config.has_section ("Node1") - Print(Hasnode) - in #checks for key-value pairs within a specified group -Haskeyvalue=config.has_option ("Node1","Key1") to Print(Haskeyvalue) + #Delete a key-value pair within a specified group -Delkeyvalue=config.remove_option ("Node2","Key2") the Print(Delkeyvalue) * #sets a key-value pair within a specified group $Config.set ("Node3","Key4","Value4")Panax Notoginseng - the #Write the modified configuration file to a file +Config.write (Open ("Node.txt","W", encoding="Utf-8"))Configparser Usage
Python--configparser