鱼C论坛

 找回密码
 立即注册
查看: 4178|回复: 3

如何解决ini中同一个section下重复的key

[复制链接]
发表于 2023-11-7 09:41:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. configparser.DuplicateOptionError: While reading from '/work_sas1/workspace/Release/build_system_iscsi/factory/MTK/factory/mt9638/factory_7T854_A23/atv/panel/7T863_A28/aq/28_85A28_N012301-008650-001/panel_audio.ini' [line 122]: option 'supportSetBTDelay' in section 'AudioBTDelay' already exists
复制代码


如上报错信息,如何将同一个section下后面扫描到的重复的key删除,或者是删除前面的保留后面的
自己代码如下
  1.     def refresh_data(self, model_dict, model_file):
  2.         self.target_dict = model_dict
  3.         self.target_file = model_file
  4.         for filename in self.target_file:
  5.             config = configparser.ConfigParser()
  6.             config.optionxform = lambda option: option
  7.             config.read(filename)
  8.             for dict_key in self.target_dict.keys():
  9.                 file_name, ini_key = dict_key.split(':')
  10.                 if config.has_option(file_name, ini_key):
  11.                     config.remove_option(file_name, ini_key)
  12.                 config.set(file_name,ini_key, str(self.target_dict[dict_key]))
  13.             with open(filename,'w') as cfgfile:
  14.                 config.write(cfgfile)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-11-7 11:12:54 | 显示全部楼层
谢谢,经分析报错是config.read(filename)的时候报错的,所以我修改了configparser.py
将原来的
  1.                         if (self._strict and
  2.                             (sectname, optname) in elements_added):
  3.                             raise DuplicateOptionError(sectname, optname,                                                                                      
  4.                                                        fpname, lineno)
  5.                         elements_added.add((sectname, optname))

  6. #修改为
  7.                         if (self._strict and
  8.                             (sectname, optname) in elements_added):
  9.                         #    raise DuplicateOptionError(sectname, optname,                                                                                      
  10.                         #                               fpname, lineno)
  11.                             self.remove_option(sectname, optname)
  12.                         else:
  13.                             elements_added.add((sectname, optname))
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-23 07:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表