配置文件如下:
import configparser conf = configparser.ConfigParser() print(type(conf)) #conf是类 conf.read('config.ini') sections = conf.sections() #获取配置文件中所有sections,sections是列表 print(sections) option = conf.options(conf.sections()[0]) #获取某个section下的所有选项或value,等价于 option = conf.options('logoninfo') print(option) #value = conf.get('logoninfo', 'addr') #根据section和value获取key值,等价于value = conf.get(conf.sections()[0], conf.options(conf.sections()[0])[0]) print(value) item = conf.items('logoninfo') print(item)
运行结果如下:
<class 'configparser.ConfigParser'>
['logoninfo', 'logging', 'mysql']
['addr', 'passwd', 'popserver']
wangershazi
[('addr', 'wangershazi'), ('passwd', 'wangerxifu'), ('popserver', 'newmail')]
Process finished with exit code 0