eightHundreds
1/1/2017 - 3:17 AM

配置文件

配置文件

import configparser
config=configparser.ConfigParser()
config['Section1']={'key1':'value1'}#写入配置
config.set('section','key','value')
config.add_section('section')


#输出

with open('config.ini','w') as f:
	config.write(f)#输出到文件
config.write(sys.stdout)#输出到标准输出流中


config.read()
config.sections() #获得所有配置段
"""
['Default','Section1']
"""
config['sectionname']['key1']
config.get('sectionname','key1',fallback='No such things')

#迭代
for section in config.sections():
	for key in config.options(section):
		print(config.get(section,key))