【主要功能】
config.json方式实现全局配置参数的读写操作
使用python实现以下功能:
1、使用将接口获取的变量值,写入到当前目录下的config文件中,如delayTime=10;
2、读取当前目录下的config文件中,特定变量的值,如delayTime=10;
3、若config文件或者节点不存在,则自动进行创建;
【详细代码】
#!/usr/bin/env python
# -*- coding: utf-8 -*-import json,osdef write_config_json(key=None, value=None,section="default", config_file='config.json'):"""将接口获取的变量值写入到config文件中:param config_file: config文件路径,默认为'config.json':param key: 需要写入的变量名:param value: 需要写入的变量值:return: 如果文件不存在则创建,如果节点不存在则新增,如果节点存在则覆盖"""if not os.path.exists(config_file):print(f'文件{config_file}不存在,将创建新的文件')with open(config_file, 'w', encoding='utf-8') as f:json.dump({f'{section}':{}}, f)with open(config_file, 'r', encoding='utf-8') as f:config = json.load(f)config[section][key] = valuewith open(config_file, 'w', encoding='utf-8') as f:json.dump(config, f, indent=4)# 封装函数:一次写入多个值至配置文件,适用于大量数据写入,提高性能
def write_configs_json(keys_values_dict, section="default", config_file='config.json'):"""将接口获取的变量值写入到config文件中接受一个变量名列表,和一个参数value,它接受一个与key列表长度相同的值列表。然后遍历这个元组列表,将每个元组的键值对写入到配置文件中。:param config_file: config文件路径,默认为'config.json':param key: 需要写入的变量名:param value: 需要写入的变量值:return: 如果文件不存在则创建,如果节点不存在则新增,如果节点存在则覆盖"""if not os.path.exists(config_file):print(f'文件{config_file}不存在,将创建新的文件')with open(config_file, 'w', encoding='utf-8') as f:json.dump({section:{}}, f)with open(config_file, 'r', encoding='utf-8') as f:config = json.load(f)if section not in config:config[section] = {}# 设置变量值for key, value in keys_values_dict.items():config[f'{section}'][key] = str(value)# 写入配置文件with open(config_file, 'w', encoding='utf-8') as f:json.dump(config, f, indent=2)def read_config_json(key=None, section="default", config_file='config.json'):"""读取config文件中的特定变量值:param config_file: config文件路径,默认为'config.json':param key: 需要读取的变量名:return: 返回读取到的变量值,如果文件或节点不存在则返回None"""if not os.path.exists(config_file):print(f'文件{config_file}不存在')return Nonewith open(config_file, 'r', encoding='utf-8') as f:config = json.load(f)if key not in config[section]:print(f'节点{key}不存在')return Nonereturn config[section][key]# 打印config文件的内容
def type_config(config_file='config.json'):# 若配置文件存在,则读取所有变量值if os.path.exists(config_file):result = open(config_file, "r", encoding='utf-8').read()print(f"result={result}")return result# 若配置文件不存在,则返回空值else:return Noneif __name__ == '__main__':# 以上代码实现了读取和写入config.json文件的功能,# 其中 read_config_json 函数用于读取特定变量的值,# write_config_json 函数用于将接口获取的变量值写入到config文件中。# 如果config文件不存在,会自动创建新的文件;# 如果节点不存在,会新增节点;# 如果节点存在,会覆盖原有的节点值。# 测试单个写入操作write_config_json('runTimes', 99)write_config_json('delayTime', 88)write_config_json('sleepTime', 66)print(f"write_config_json={type_config()}")# 测试批量写入操作my_section = {"key1": "value1","key2": "value2","key3": "value3"}write_configs_json(my_section,"HRD")print(f"write_configs_json={type_config()}")# 测试读取操作value = read_config_json('delayTime')print(value)
【运行效果】
result={"default": {"runTimes": 99,"delayTime": 88,"sleepTime": 66},"my_section": {"key1": "value1","key2": "value2","key3": "value3"},"HRD": {"key1": "value1","key2": "value2","key3": "value3"}
}
write_config_json={"default": {"runTimes": 99,"delayTime": 88,"sleepTime": 66},"my_section": {"key1": "value1","key2": "value2","key3": "value3"},"HRD": {"key1": "value1","key2": "value2","key3": "value3"}
}
result={"default": {"runTimes": 99,"delayTime": 88,"sleepTime": 66},"my_section": {"key1": "value1","key2": "value2","key3": "value3"},"HRD": {"key1": "value1","key2": "value2","key3": "value3"}
}
write_configs_json={"default": {"runTimes": 99,"delayTime": 88,"sleepTime": 66},"my_section": {"key1": "value1","key2": "value2","key3": "value3"},"HRD": {"key1": "value1","key2": "value2","key3": "value3"}
}
88