python配置⽂件封装_Python中configparser配置⽂件的读写及
封装,配置。。。
#封装配置⽂件
from configparser importConfigParserclassHandleConfig:"""配置⽂件读写数据的封装"""
def __init__(self, filename):""":param filename: 配置⽂件名"""self.filename=filename
读取配置⽂件1.创建配置解析器
exo四大私生饭
李心洁微博#get_value获取所有的字符串,section区域名, option选项名
defget_value(self, section, option):(section, option)#get_int获取整型,section区域名, option选项名
defget_int(self, section, option):int(section, option)#get_float获取浮点数类型,section区域名, option选项名
defget_float(self, section, option):float(section, option)#get_boolean(译:⽐例恩)获取布尔类型,section 区域名, option选项名
defget_boolean(self, section, option):boolean(section, option)#get_eval_data 获取列表,section区域名, option选项名
defget_eval_data(self, section, option):(section, option))
@staticmethoddefwrite_config(datas, filename):"""写⼊配置操作
:param datas: 需要传⼊写⼊的数据
:param filename: 指定⽂件名
:return:"""
洛克人x8#做校验,为嵌套字典的字典才可以(意思.隐私.谈.ce)
任月丽近况
if isinstance(datas, dict): #遍历,在外层判断是否为字典
#再来判断内层的 values 是否为字典
读取配置文件失败
for value in datas.values(): #先取出value
if not isinstance(value, dict): #在判断
return "数据不合法, 应为嵌套字典的字典"config= ConfigParser() #1.创建配置解析器---与写⼊配置操作⼀致
for key in datas: #写⼊操作
config[key] =datas[key]     # config 类似于⼀个空字典
with open(filename,"w") as file: #保存到哪个⽂件filename=需要指定⽂件名
config.write(file)#return "写⼊成功"
#do_config = HandleConfig('f')
if __name__ == '__main__':#读取操作
do_config1 = HandleConfig('f') #读取那个⽂件
res = _value("msg", "success_result") #读取什么内容
print(res)#写⼊操作
do_config = HandleConfig('write_config.ini')
datas={"file path": {"cases_path": "cases.xlsx","log_path": "record_"},"msg": {"success_result": "Pass","fail_result": "Fail"}
}
do_config.write_config(datas,"write_config.ini")pass
>邓紫棋不雅照