|

楼主 |
发表于 2023-10-6 16:03:30
|
显示全部楼层
本帖最后由 liyuping-fisher 于 2023-10-6 16:12 编辑
- def refresh_data(self, model_dict, model_file):
- # 传参分别为派生屏的模块字典和派生屏的路径
- self.target_dict = model_dict
- self.target_file = model_file
- for filename in self.target_file: # 遍历文件名列表
- if not os.path.exists(filename): # 检查文件是否存在
- print(f"文件 {filename} 不存在!")
- continue
-
- # 初始化一个config对象
- config = configparser.ConfigParser()
-
- # 读取配置文件
- config.read(filename)
-
- # 找到ini文件中key所在的section
- for dict_key in self.target_dict.keys():
- file_name, ini_key = dict_key.split(':') # 分割得到文件名和key
- for section in config.sections():
- options = config.options(section)
- for key, value in config.items(section): # 获取section中的键值对
- if key.lower() == ini_key.lower(): # 不区分大小写比较key
- config.set(section, key, self.target_dict[dict_key]) # 替换value
-
- # 将修改后的配置写回文件
- with open(filename, 'w') as configfile:
- config.write(configfile)
-
-
- proc_dervie_data.refresh_data(new_factoryroot_data_dict, derive_factoryroot_file)
- 这样写了后替换的key变成了小写,字典内容是{'sys.ini:MAX_MODEL_INDEX': '43', 'sys.ini:MAX_PROJECT_ID': '43'} 替换后内容成了max_model_index = 43 max_project_id = 43
复制代码
不但替换的key变成小写了,ini文件的所有key都变成了小写 |
|