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

Source: Internet
Author: User

 

Python

In practical applications, NLP 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:

[DB]

Db_host = 127.0.0.1

DB Port = 3306

Db_user = root

Db_pass = Password

[Concurrent]

Thread = 10

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:

#-*-Encoding: gb2312 -*-
Import configparser
Import string, OS, sys

If 1:
_ DEBUG = true
If _ DEBUG = true:
Import PDB
PDB. set_trace ()

Cf = configparser. configparser ()
Cf. Read ("test. conf ")

# Return all sections
S = Cf. Sections ()
Print 'section: ', S
O = Cf. Options ("DB ")
Print 'Options: ', O
V = Cf. Items ("DB ")
Print 'db: ', V
Print '-' * 60
# Read data by type
Db_host = Cf. Get ("DB", "db_host ")
Db_port = Cf. getint ("DB", "db_port ")
Db_user = Cf. Get ("DB", "db_user ")
Db_pass = Cf. Get ("DB", "db_pass ")
# The returned result is an integer.
Threads = Cf. getint ("concurrent", "Thread ")
Processors = Cf. getint ("concurrent", "processor ")
Print "db_host:", db_host
Print "db_port:", db_port
Print "db_user:", db_user
Print "db_pass:", db_pass
Print "thread:", threads
Print "processor:", processors
# Modify a value and write it back.
Cf. Set ("DB", "db_pass", "zhaowei ")
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.