configparser模块

configparser

import configparser

config = configparser.ConfigParser()
config.read('test.ini')
import configparser

config = configparser.ConfigParser()
config.read('test.ini')

# 1.获取sections
print(config.sections())    # ['section1', 'section2']

# 2.获取某一sections下的所有的option
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.获取某个section单独的元素值
res = config.get('section1', 'user')
print(res, type(res))   # egon <class 'str'>

res1 = config.getint('section1', 'age')
print(res1, type(res1)) # 18 <class 'int'>

res2 = config.getboolean('section1', 'is_admin')
print(res2, type(res2)) # True <class 'bool'>

res3 = config.getfloat('section1', 'salary')
print(res3, type(res3)) # 31.0 <class 'float'>

 

上一篇:20201103~Config文件读取


下一篇:Latex 篇章结构