Python--configparser

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.