configparser模块
配置文件(.ini)
[section1] k1 = v1 k2:v2 user=egon age=18 is_admin=true salary=31 [section2] k1 = v1
查
import configparser config = configparser.ConfigParser() config.read('text.ini') # 获取sections res = config.sections() print(res) # ['section1', 'section2'] # 获取某一section下的所有option print(config.options('section1')) # ['k1', 'k2', 'user', 'age', 'is_admin', 'salary'] # 获取items print(config.items('section1')) # [('k1', 'v1'), ('k2', 'v2'), ('user', 'egon'), ('age', '18'), ('is_admin', 'true'), ('salary', '31')] # 获取某个section单独的元素值 res = config.get('section1', 'user') print(res, type(res)) # egon <class 'str'> # 查看标题section1下age的值=>整数格式 res = config.getint('section1', 'age') print(res, type(res)) # 18 <class 'int'> # 查看标题section1下is_admin的值=>布尔值格式 res = config.getboolean('section1', 'is_admin') print(res, type(res)) # True <class 'bool'> # 查看标题section1下salary的值=>浮点型格式 res = config.getfloat('section1', 'salary') print(res, type(res)) # 31.0 <class 'float'>
增删改
import configparser config = configparser.ConfigParser() config.read('text.ini') # 删除整个标题section2 config.remove_section('section2') # 删除标题section1下的某个k1和k2 config.remove_option('section1', 'k1') config.remove_option('section1', 'k2') # 判断是否存在某个标题 print(config.has_section('section1')) # 判断标题section1下是否有user print(config.has_option('section1', 'user')) # 添加一个标题 config.add_section('section3') # 在标题section3下添加name=egon,age=18的配置 config.set('section3', 'name', 'egon') config.set('section3', 'age', '18') # 最后将修改的内容写入文件,完成最终的修改 config.write(open('a.cfg', 'w'))
import configparser config = configparser.ConfigParser() config["DEFAULT"] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9'} config['bitbucket.org'] = {} config['bitbucket.org']['User'] = 'hg' config['topsecret.server.com'] = {} topsecret = config['topsecret.server.com'] topsecret['Host Port'] = '50022' # mutates the parser topsecret['ForwardX11'] = 'no' # same here config['DEFAULT']['ForwardX11'] = 'yes' with open('example.ini', 'w') as configfile: config.write(configfile)
import configparser # 生成mysql配置信息 conf_obj = configparser.ConfigParser # con_obj['配置标题'] = [配置字典] conf_obj['MYSQL'] = {'HOST': '127.0.0.1', 'PORT': '3306', 'USER': 'tank', 'PASSWORD': '123456', } with open('mysql.ini', 'w') as f: conf_obj.write(f) # 校验mysql配置信息 conf_obj = configparser.ConfigParser() # 读取mysql.ini配置文件 conf_obj.read('mysql.ini') title = conf_obj.sections() if 'MYSQL' in title: ini_user = conf_obj['MYSQL']['USER'] ini_pwd = conf_obj['MYSQL']['PASSWORD'] if ini_user == 'tank' and ini_pwd == '123456': print('mysql连接成功!')
[settings] # 获取项目根目录Novel-1的根目录 BASE_PATH = os.path.dirname(os.path.dirname(__file__)) # 获取db目录的路径 DB_PATH = os.path.join(BASE_PATH, 'db') # 获取db.txt的根目录 DB_TXT_PATH = os.path.join(DB_PATH, 'db.txt') # story_class文件目录路径 STORY_PATH = os.path.join(DB_PATH, 'story_class.txt') # 小说存放目录 FICTION_DIR = os.path.join(DB_PATH, 'fictions') # 日志文件的路径 LOG_PATH = os.path.join(BASE_PATH, 'log', 'log.txt')

浙公网安备 33010602011771号