The Configparser of Python

Source: Internet
Author: User

I. Introduction of Configparser

Configparser is the package that is used to read the configuration file. The format of the configuration file is as follows: the brackets "[]" are included in the section. section below is a configuration content similar to Key-value.

1: [db]

2:db_host = 127.0.0.1

3:db_port =

4:db_user = root

5:db_pass = Rootroot

6:

7: [Concurrent]

8:thread = Ten

9:processor =

The brackets "[]" are included in section. The section is followed by the configuration of options similar to Key-value.

Ii. Initial work of Configparser

Using Configparser preferred to initialize the instance and read the configuration file:

1:CF = Configparser.configparser ()

2:cf.read ("Profile name")

Iii. Common methods of Configparser

1. Get all sections. That is, all "[]" in the configuration file is read to the list:

1:s = cf.sections ()

2:print ' section: ', S

Output (The following will be the example of profiles in profile):

1:section: [' db ', ' concurrent ']

2. Gets the options for the specifiedsection. To read a key in a section of the configuration file into the list:

1:o = cf.options ("db")

2:print ' options: ', O

Will output:

1:options: [' db_host ', ' db_port ', ' db_user ', ' Db_pass ']

3. Gets the configuration information for the specified section.

1:v = Cf.items ("db")

2:print ' db: ', v

Will output:

1:db: [(' Db_host ', ' 127.0.0.1 '), (' Db_port ', ' ') ', (' Db_user ', ' Root '), (' Db_pass ', ' rootroot ')]

4. Read the option information for the specified section by type.

There are also getfloat and Getboolean.

1: #可以按照类型读取出来

2:db_host = Cf.get ("db", "Db_host")

3:db_port = Cf.getint ("db", "Db_port")

4:db_user = Cf.get ("db", "Db_user")

5:db_pass = Cf.get ("db", "Db_pass")

6:

7: # Returns the integer type

8:threads = cf.getint ("Concurrent", "thread")

9:processors = cf.getint ("Concurrent", "Processor")

Ten:

11:print "Db_host:", Db_host

12:print "Db_port:", Db_port

13:print "Db_user:", Db_user

14:print "Db_pass:", Db_pass

15:print "Thread:", Threads

16:print "Processor:", processors

Will output:

1:db_host:127.0.0.1

2:db_port:22

3:db_user:root

4:db_pass:rootroot

5:thread:10

6:processor:20

5. Set the value of an option . (Remember to write back at the end)

1:cf.set ("db", "Db_pass", "Zhaowei")

2:cf.write (Open ("test.conf", "W"))

6. Add asection. (also to write back)

1:cf.add_section (' liuqing ')

2:cf.set (' liuqing ', ' int ', ' a ')

3:cf.set (' liuqing ', ' bool ', ' true ')

4:cf.set (' liuqing ', ' float ', ' 3.1415 ')

5:cf.set (' liuqing ', ' baz ', ' fun ')

6:cf.set (' liuqing ', ' Bar ', ' Python ')

7:cf.set (' liuqing ', ' foo ', '% (bar) s is% (baz) s! ')

8:cf.write (Open ("test.conf", "W"))

7. Remove section or option. (as long as the changes will be written back to the OH)

1:cf.remove_option (' liuqing ', ' int ')

2:cf.remove_section (' liuqing ')

3:cf.write (Open ("test.conf", "w"))

Click (here) to collapse or open

  1. #!/usr/bin/env python
  2. From Configparser import Configparser
  3. Configfile= "F.txt"
  4. Config=configparser ()
  5. Config.read (ConfigFile)
  6. Print Config.get (' Messages ', ' greeting ')
  7. Radius=input (Config.get (' Messages ', ' questions ') + ')
  8. Print Config.get (' Messages ', ' result ')
  9. Print config.getfloat (' numbers ', ' pi ') *radius**2
  10. S=config.sections ()
  11. print ' section: ', S
  12. O=config.options (' Messages ')
  13. print ' Messages option: ', O
  14. V=config.items ("Messages")
  15. print ' Message de Xinxi: ', V
  16. Config.add_section (' liuyang1 ')
  17. Config.set (' liuyang1 ', ' int ', ' 15 ')
  18. Config.set (' Liuyang ' 1, ' hhhh ', ' Hello World ')
  19. Config.write (Open ("F.txt", "w"))
  20. Print Config.get (' liuyang1 ', ' int ')
  21. Print Config.get (' liuyang1 ', ' hhhh ')
  22. #!/usr/bin/env python
  23. Import Configparser
  24. Import Sys
  25. Config=configparser.configparser ()
  26. Config.add_section ("Book1")
  27. Config.set ("Book1", "title", "Hello World")
  28. Config.set ("Book1", "aut", "log")
  29. Config.write (Open ("F.txt", "w"))

Python's Configparser

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.