Python module configuration file Configparser (varies significantly in python3)

Source: Internet
Author: User

#-*-coding:utf-8-*-#python#Xiaodeng#python module Configparser (configparser in Python3)#Special Note: Python3 and python2 are very different about the function usage of the module.#configuration file ParserImportConfigparser,os#initializing a configuration file objectconfig=Configparser.configparser ()#Add a sectionConfig.add_section ('Section01') config.add_section ('Section02')#Add attribute key value pairs to sectionConfig.set ('Section01','name','Xiaodeng')#Set (section, option, value)Config.set ('Section01',' Age',' -') Config.set ('Section01','Bar','python') Config.set ('Section01','An_int',' the') Config.set ('Section01','A_bool','true') Config.set ('Section01','a_float','3.1415') Config.set ('Section01','Baz',' Fun') Config.set ('Section01','Bar','Fengmei') Config.set ('Section01','Foo','% (bar) s is% (baz) s!') Config.set ('Section01',' Age',' -') Config.set ('Section02','Bar','Python') Config.set ('Section02','Bar','python')#Write FileWith open ('test.cfg','WB') as ConfigFile:config.write (configfile)#Read cfg fileconfig=Configparser.configparser () config.read ('test.cfg')#Configparser.configparser Instance#Take valuePrintConfig.getfloat ('Section01','a_float')#getfloat (section, options)PrintConfig.getint ('Section01','An_int')#getint (section, options)PrintConfig.getboolean ('Section01','A_bool')#Getboolean (section, options)#returns a list-form tuple; Items (section, Raw=false, Vars=none)PrintConfig.items ('Section01')#[(' Name ', ' Xiaodeng '), (' Age ', ' ' + '), (' Bar ', ' Fengmei '), (' An_int ', ' ') ', (' A_bool ', ' true '), (' A_float ', ' 3.1415 ') , (' Baz ', ' fun '), (' foo ', ' Fengmei is fun! ')]#take all the section namesPrintConfig.sections ()#[' Section01 ', ' Section02 ']#get all the configuration table names keyPrintConfig.options ('Section01')#[' name ', ' age ', ' bar ', ' an_int ', ' a_bool ', ' a_float ', ' Baz ', ' foo ']#Delete the content under the specified section optionConfig.remove_section ('Section02')#There is still Section02 in the view file when testing, but only Section01 when you view sections's name with an instruction#Get (section, option, Raw=false, Vars=none)#finds the value of a specific option in the specified section option#Note: The default is to find the last value of the same optionPrintConfig.get ('Section01','Bar')PrintConfig.get ('Section01',' Age')PrintConfig.get ('Section01','Bar')" "Configparser--Responsible for parsing a list of configuration files, and managing the parsed database            .  Methods: __init__ (Defaults=none) Create the parser and specify a dictionary of intrinsic defaults.  The keys must be strings, the values must is appropriate for% () s string interpolation.            Note that ' __name__ ' are always a intrinsic default;            Its value was the section ' s name. Sections () #取所有的section名字 return all of the configuration section names, sans DEFAULT has_sectio N (section) return whether the given sections exists has_option (section, option) return whe  Ther the given option exists in the given sections options (section) #获取所有的配置表名字key return list of configuration options for the named section read (filenames) Read and parse the list of named con          figuration files, given by  Name.  A single filename is also allowed.  non-existing files are ignored.            Return List of successfully read files.            READFP (FP, Filename=none) Read and parse one configuration file, given as a file object. The filename defaults to Fp.name; It is only used in error messages (if FP have no ' name ' attribute, the string ' <???            > ' is used).  Get (section, option, Raw=false, Vars=none) return a string value for the named option. All% interpolations is expanded in the return values, based on the defaults passed into the Constr  Uctor and the DEFAULT section.            Additional substitutions May is provided using the ' vars ' argument, which must be a dictionary whose            Contents override any pre-existing defaults.            Getint (section, options) like get (), but convert value to an integer getfloat (section, options) Like get (), but convert value to A float getboolean (section, options) like get (), but convert value to a Boolean (currently case  Insensitively defined as 0, false, no, off for false, and 1, true, yes, on for true).            Returns False or True. Items (section, Raw=false, Vars=none) return a list of tuples with (name, value) for each option in t            He section. Remove_section (section) remove the given file section and all its options remove_option (section, opt ION) Remove the given option from the given sections set (section, option, value) set the G             Iven option Write (FP) write the configuration state in. ini format add_section Method of Configparser.configparser instance Create a new section in the configuration." "

Python module configuration file Configparser (varies significantly in python3)

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.