In-depth discussion on the correct application mode of Python ConfigParser Module

Source: Internet
Author: User

In practical applications, the Python programming language idea helps us create many useful values. It is a simple object-oriented programming language, which contains many useful modules for us to use. Today, we will introduce the application of one of the most important Python ConfigParser modules.

It is common to use a configuration file in a program to flexibly configure some parameters. The parsing of configuration files is not complicated, especially in Python, the officially released library contains the library for doing this, that is, ConfigParser. Here is a brief introduction.

The format of the configuration file parsed by the Python ConfigParser module is similar to that of the ini configuration file, which consists of multiple sections. Each section has multiple configuration items, such:

 
 
  1. [db]  
  2. db_host=127.0.0.1  
  3. db_port=3306 
  4. db_user=root 
  5. db_pass=password 
  6. [concurrent]  
  7. thread=10 
  8. processor=20 

Assume that the configuration file above is named test. conf. It contains two sections, one is db, the other is concurrent, the db also contains four items, and the concurrent contains two items. Here we will do the parsing:

 
 
  1. #-*-Encoding: gb2312 -*-
  2. Import ConfigParser
  3. Import string, OS, sys
  4. Cf = ConfigParser. ConfigParser ()
  5. Cf. read ("test. conf ")
  6. # Return all sections
  7. S = cf. sections ()
  8. Print 'section: ', s
  9. O = cf. options ("db ")
  10. Print 'Options: ', o
  11. V = cf. items ("db ")
  12. Print 'db: ', v
  13. Print '-' * 60
  14. # Read data by type
  15. Db_host = cf. get ("db", "db_host ")
  16. Db_port = cf. getint ("db", "db_port ")
  17. Db_user = cf. get ("db", "db_user ")
  18. Db_pass = cf. get ("db", "db_pass ")
  19. # The returned result is an integer.
  20. Threads = cf. getint ("concurrent", "thread ")
  21. Processors = cf. getint ("concurrent", "processor ")
  22. Print "db_host:", db_host
  23. Print "db_port:", db_port
  24. Print "db_user:", db_user
  25. Print "db_pass:", db_pass
  26. Print "thread:", threads
  27. Print "processor:", processors
  28. # Modify a value and write it back.
  29. Cf. set ("db", "db_pass", "zhaowei ")
  30. Cf. write (open ("test. conf", "w "))

The preceding section describes the application methods of the Python ConfigParser 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.