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.