加载配置文件获取相关配置
import configparser
config=configparser.ConfigParser()
# test.ini为相对路径的配置文件
config.read('test.ini')
test.ini内容
# 注释1
; 注释2
[section1]
k1 = v1
k2:v2
user=egon
age=18
is_admin=true
salary=31
[section2]
k1 = v1
import configparser
config=configparser.ConfigParser()
config.read('test.ini')
# 1、获取sections
# print(config.sections()) # ['section1', 'section2']
# 2、获取某一section下的所有options
# print(config.options('section1')) # ['k1', 'k2', 'user', 'age', 'is_admin', 'salary']
# 3、获取items
# print(config.items('section1')) # [('k1', 'v1'), ('k2', 'v2'), ('user', 'egon'), ('age', '18'), ('is_admin', 'true'), ('salary', '31')]
# 4、
# res=config.get('section1','user')
# print(res,type(res))
# egon <class 'str'>
# 5
# res=config.getint('section1','age')
# print(res,type(res))
# 18 <class 'int'>
# res=config.getboolean('section1','is_admin')
# print(res,type(res))
# True <class 'bool'>
# res=config.getfloat('section1','salary')
# print(res,type(res))
# 31.0 <class 'float'>