600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > Python模块configparser:加载配置文件config.ini

Python模块configparser:加载配置文件config.ini

时间:2024-04-04 05:32:29

相关推荐

Python模块configparser:加载配置文件config.ini

目录结构:

config.ini

load_config.py

import configparser''' 1.读取配置文件 '''config = configparser.ConfigParser()config.read('config.ini') # 读取configprint('='*40)''' 2.输出所有section '''print(config.sections())print('='*40)''' 3.输出某个section的配置项 '''rootPath = config['rootPath']['root']print('rootPath: %s' % rootPath)print('='*40)''' 4.添加/删除section '''config.add_section('others')print(config.sections())config.remove_section('others')print(config.sections())print('='*40)''' 5.添加/删除配置项 '''config.set(section='modelPath', option='distilbert-base-uncased', value='distilbert-base-uncased')config.set(section='modelPath', option='distilbert-base-uncased-temp', value='distilbert-base-uncased')print(list(config['modelPath']))config.remove_option(section='modelPath', option="distilbert-base-uncased-temp")print(list(config['modelPath']))''' 6.save '''with open('config.ini', 'w') as f:config.write(f)

输出结果:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。