python-configparser module reads the configuration file

Original link: https://www.cnblogs.com/ming5218/p/7965973.html

A, ConfigParser Profile

ConfigParser package is used to read the configuration file. Configuration file format is as follows: in the brackets "[]" is included within the section. The following is a section similar to the configuration of the key-value content.

[db]
db_host = 127.0.0.1
db_port = 69
db_user = root
db_pass = root
host_port = 69

[concurrent]
thread = 10
processor = 20 

Three, ConfigParser common method

1, with node acquisition section

# Get the node section used 
Import ConfigParser 
config = configparser.ConfigParser () 
config.read ( " INI " , encoding = " UTF-. 8 " )
 Print (config.sections ())
 # Run Results 
# [ 'DB', 'Concurrent ']

2, to obtain a specified section of the options . Within the configuration file is about to read a section key to the list:

import configparser
config = configparser.ConfigParser()
config.read("ini", encoding="utf-8")
r = config.options("db")
print(r)
#运行结果
# ['db_host', 'db_port', 'db_user', 'db_pass', 'host_port']

3, to get the value of the option under the guidance of the guidance section

Import ConfigParser 
config = configparser.ConfigParser () 
config.read ( " INI " , encoding = " UTF-. 8 " ) 
R & lt = config.get ( " DB " , " DB_HOST " )
 # R1 = config.getint ( "DB", "k1") # value of the acquired converted to an int 
# R2 = config.getboolean ( "DB", "K2") # convert the acquired value to bool 
# R3 = config.getfloat ( "DB", "K3 ") # the acquired value is converted to floating- 
Print (r)
 # operating results 
# 127.0.0.1

4, the acquiring section pointing configuration information

import configparser
config = configparser.ConfigParser()
config.read("ini", encoding="utf-8")
r = config.items("db")
print(r)
#运行结果
#[('db_host', '127.0.0.1'), ('db_port', '69'), ('db_user', 'root'), ('db_pass', 'root'), ('host_port', '69')]

5, modify the value of an option, if there is no will to create a

# Modify the value of an option, if the option does not exist it will be created 
Import ConfigParser 
config = configparser.ConfigParser () 
config.read ( " the INI " , encoding = " UTF-8 " ) 
config.set ( " db " , " db_port " , " 69 " ) # db_port modified value 69 
config.write (Open ( " INI " , " W " ))

6, check the section or if there is option, bool value

Import ConfigParser 
config = configparser.ConfigParser () 
config.has_section ( " Section " ) # the presence of the Section 
config.has_option ( " Section " , " option " )   # the existence of the option

7, and the option to add section

import configparser
config = configparser.ConfigParser()
config.read("ini", encoding="utf-8")
if not config.has_section("default"):  # 检查是否存在section
    config.add_section("default")
if not config.has_option("default", "db_host"):  # 检查是否存在该option
    config.set("default", "db_host", "1.1.1.1")
config.write(open("ini", "w"))

8, and the option to delete section

import configparser
config = configparser.ConfigParser()
config.read("ini", encoding="utf-8")
config.remove_section("default") #整个section下的所有内容都将删除
config.write(open("ini", "w"))
operation result
[db]
db_host = 127.0.0.1
db_port = 69
db_user = root
db_pass = root

[concurrent]
thread = 10
processor = 20

9, write to the file

The following lines of code just to read the contents of a file into memory, and then into a series of operations must write the file to take effect.

import configparser
config = configparser.ConfigParser()
config.read("ini", encoding="utf-8")

Write-back file as follows :( use the write method of configparser)

config.write(open("ini", "w"))

 

Two, ConfigParser initialize the object

Use ConfigParser choice needs to be initialized instance, and read the configuration file:

import configparser
config = configparser.ConfigParser()
config.read("ini", encoding="utf-8")

Three, ConfigParser common method

1, with node acquisition section

# 获取所用的section节点
import configparser
config = configparser.ConfigParser()
config.read("ini", encoding="utf-8")
print(config.sections())
#运行结果
# ['db', 'concurrent']

Guess you like

Origin www.cnblogs.com/wsnan/p/11387678.html