Configuration file parsing module-configparser in Python

Source: Internet
Author: User

Python has the Configparser class, which makes it easy to read data from a configuration file (such as the configuration of a db, the configuration of a path).
The format of the configuration file is: [] contains a key called section, the section has Option=value and so on
Common methods of this module
1, Config=configparser.configparser ()
Creating an Configparser instance
2, Config.sections ()
Returns a sequence of sections in a configuration file
3. Config.options (section)
Returns a sequence of all the keys in a project
4, Config.get (section,option)
Returns the key value of option in the section stanza
5, config.add_section (str)
Add a configuration file node (str)
6, Config.set (Section,option,val)
Set the value of the key named option (Val) in the section node
7. Config.read (filename)
Reading configuration Files
8, Config.write (Obj_file)
Write a configuration file

Example: Configuration file test.conf [section1]name = tankage = 28[section2]ip = 192.168.1.1port = 8080 code example: #-*-coding:utf-8-*-Import configparserconf = Configparser.configparser ()------Create an Object Conf.read ("c:\\test.conf") # Gets the specified section, The value of the specified option name = Conf.get ("Section1", "name") print (name) age = Conf.get ("Section1", "Age") print age# Gets all the section-----output as an array sections = Conf.sections () print sections# write config file # Update the value of the specified section, option Conf.set ("Section2", " Port "," 8081 ") # writes the specified section, adds the value of the new option Conf.set (" Section2 "," Ieport "," 80 ") # Add a new Sectionconf.add_section (" New_ Section ") Conf.set (" New_section "," New_option "," Http://www.cnblogs.com/tankxiao ") # Write back the profile conf.write (open (" c:\\ Test.conf "," W "))-------remember the changes to the configuration file and must be written to the configuration file via the Write method, otherwise invalid

  

Configuration file parsing module-configparser in Python

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.