其他分享
首页 > 其他分享> > configparser模块

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'>

 

标签:config,模块,res,print,type,section1,configparser
来源: https://www.cnblogs.com/zhww/p/12984031.html