Configparse module of the Python module

Source: Internet
Author: User

1 #-*-coding:utf-8-*-2 ImportConfigparser3 #Configparser Module4 5 #1. Create a Create object6 #2. Create a configuration item dictionary that creates a key-value pair7 #3. Create files to write configuration entries8 9Config =Configparser. Configparser ()Ten  One #Create method One: Aconfig["DEFAULT"] = { -     'Serveraliveinternel':' $', -     'conpression':'Yes', the     'Conpressionlevel':'9' - } -  - #Create method Two: +config['dabaojian.org'] = {} -config['dabaojian.org']['Host'] ='11.4.10.20' +config['dabaojian.org']['Port'] ='8080' Aconfig['dabaojian.org']['Zone'] ='Ky1' at  - #Create method Three: -  -config["mctianyou"] = {} -A =config["mctianyou"] -a["a"] ='m' ina["b"] ="v" -a["C"] ="op" to  +  -  theWith open ("Testconfig.ini","W") as ConfigFile: *Config.write (ConfigFile)#write content to the created file $ Panax Notoginseng  - #read the contents of the configuration file: the #Read () method + #Get a value A  theConfig.read ("Testconfig.ini") + Print(Config.sections ())#can only come up with non-default modules - Print(Config.defaults ())#remove data from the default module only $g=config["DEFAULT"]["conpression"] $ Print(g) -  - #a special attribute of default is as follows: the #He's going to print out the key values in the default module. -  forKeyinchconfig['dabaojian.org'] :Wuyi     Print(Key) the  -  Wu #changes to the configuration file: - #a very important feature of the file is that when the file is generated and cannot be modified, a new file must be regenerated after the contents of the file are modified to About #overwrite the original file to $  - #deletion of values in a file - #to delete a key-value pair under a configuration module -Config.remove_section ("mctianyou")#Delete a file by key value AConfig.write (Open ("Testconfig.ini","W")) +Config.remove_option ("DEFAULT","Conpressionlevel") the  - #Determine if you have this key value $Flag = Config.has_section ("mctianyou") the Print(flag)#False the  the #modification of configuration file values theConfig.set ("DEFAULT","conpression","No")

Configparse module of the Python module

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.