Python configparser use

Source: Internet
Author: User

An. ini file consists of several sections (parts), and each section consists of several key-value pairs.

Take Example.ini as an example:

[DEFAULT] serveraliveinterval   =   Compression  =  Yes compressionlevel  =  9 ForwardX11  =   yes[bitbucket.org] User   =  hg[topsecret.server.com] Port  =  50022 ForwardX11  =  No

Create an. ini file

ImportConfigparser#CONFIGD objects are similar to dictionariesConfig =Configparser. Configparser () config["DEFAULT"] = {    "Serveraliveinterval": 45,    "comperssion":"Yes",    "Comperssionlevel"8 {,    "ForwardX11":"Yes"}config["bitbucket.org"] ={}config["bitbucket.org"]["User"] ="HG"config["topsecret.server.com"] ={}config["topsecret.server.com"]["Port"] ="50022"config["topsecret.server.com"]["ForwardX11"] ="No"With Open ("Example.ini","W") as FP:#Write FileConfig.write (FP)

Read the. ini file

Config = Configparser. Configparser () config.read ("example.ini")

About Section

ImportConfigparserconfig=Configparser. Configparser () Config.read ("Example.ini")#get the section name listPrint(Config.sections ())#[' bitbucket.org ', ' topsecret.server.com ']#get the default section namePrint(config.default_section)#DEFAULT#get the default section objectPrint(Config.defaults ())#ordereddict (' Serveraliveinterval ', ' a '), (' comperssion ', ' yes '), (' Comperssionlevel ', ' 9 '), (' forwardx11 ', ' Yes ')])#determine if the corresponding section is includedPrint('bitbucket.org' inchconfig)Print(Config.has_section ("example.com"))#2 ways to add a sectionconfig['example.com'] ={}config['example.com']['username'] ='python'Config.add_section ("example.com1") config.write (open ("Tmp.ini","W"))#2 ways to delete a sectiondelconfig['example.com']config.remove_section ("example.com") config.write (open ("Tmp.ini","W"))

About option

ImportConfigparserconfig=Configparser. Configparser () Config.read ("Example.ini")#the option here corresponds to key#get the key list for section correspondingBitbucket_keys = Config.options ('bitbucket.org')Print(Bitbucket_keys)#[' User ', ' serveraliveinterval ', ' comperssion ', ' comperssionlevel ', ' forwardx11 ']#determine if there is a corresponding key in the sectionPrint(Config.has_option ("bitbucket.org","User"))Print('User1' inchConfig.options ('bitbucket.org'))Print('User1' inchconfig['bitbucket.org'])#Add Optionconfig['bitbucket.org']['pwd'] ='python'config.write (Open ("Tmp.ini","W"))#2 ways to remove optiondelconfig['bitbucket.org']['User']config.remove_option ("bitbucket.org","User") config.write (open ("Tmp.ini","W"))

Value related

ImportConfigparserconfig=Configparser. Configparser () Config.read ("Example.ini")#2 ways to get the value of keyuser = config['bitbucket.org']['User']user= Config.get ('bitbucket.org','User')Print(user)#HGinterval= Config.getint ("topsecret.server.com","Port")Print(interval)# $Compression = Config.getboolean ("topsecret.server.com","ForwardX11")Print(compression)#and Config.getfloat ()#2 ways to set valueConfig.set ('bitbucket.org','User','Peter') config['bitbucket.org']["User"] ="Peter"config.write (Open ("Tmp.ini","W"))

Python configparser use

Related Article

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.