鱼C论坛

 找回密码
 立即注册
查看: 3134|回复: 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 09:41:35 | 显示全部楼层
问题:如何在Python中处理INI文件中同一个section下重复的key?

当在使用 configparser 读取INI文件时出现 DuplicateOptionError 错误,表示在同一个section下存在重复的key,解决方法可以通过以下步骤实现:

1. 保留后面的重复key并删除前面的重复key:
  1. def remove_duplicate_keys(config, section):
  2.     # 获取当前section的所有option
  3.     options = config.options(section)
  4.     seen = set()  # 用于记录已经出现过的key
  5.     for option in options[::-1]:  # 倒序遍历options
  6.         if option in seen:
  7.             config.remove_option(section, option)  # 删除重复的key
  8.         else:
  9.             seen.add(option)  # 将未出现过的key加入seen集合
复制代码

在你的代码中,你可以在读取INI文件后,对每个section调用 remove_duplicate_keys 函数来处理重复的key。具体调用方法如下:
  1. for filename in self.target_file:
  2.     config = configparser.ConfigParser()
  3.     config.optionxform = lambda option: option
  4.     config.read(filename)
  5.     for section in config.sections():
  6.         remove_duplicate_keys(config, section)  # 处理重复的key
  7.         for dict_key in self.target_dict.keys():
  8.             file_name, ini_key = dict_key.split(':')
  9.             if config.has_option(file_name, ini_key):
  10.                 config.remove_option(file_name, ini_key)
  11.             config.set(file_name, ini_key, str(self.target_dict[dict_key]))
  12.     with open(filename, 'w') as cfgfile:
  13.         config.write(cfgfile)
复制代码

通过以上修改,你的代码应该能够正确处理INI文件中同一个section下的重复key了。

球一个最佳答案谢谢啦!这对我非常重要!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-7 09:47:16 | 显示全部楼层
在处理INI文件时,可以使用 configparser 模块来读取和修改配置文件。对于重复的key,可以使用 remove_option() 方法来删除前面的key,保留后面的key。

在你的代码中,你已经使用了 remove_option() 方法来删除重复的key,但是你需要将其放在正确的位置。在遍历 target_dict 之前,你应该先读取配置文件并创建 config 对象。然后,在遍历 target_dict 时,检查是否存在重复的key,并在存在时删除它。最后,将更新后的配置文件写回到文件中。

下面是修正后的代码:

  1. import configparser

  2. def refresh_data(model_dict, model_file):
  3.     target_dict = model_dict
  4.     target_file = model_file

  5.     for filename in target_file:
  6.         config = configparser.ConfigParser()
  7.         config.optionxform = lambda option: option
  8.         config.read(filename)

  9.         for dict_key in target_dict.keys():
  10.             file_name, ini_key = dict_key.split(':')
  11.             if config.has_option(file_name, ini_key):
  12.                 config.remove_option(file_name, ini_key)
  13.             config.set(file_name, ini_key, str(target_dict[dict_key]))

  14.         with open(filename, 'w') as cfgfile:
  15.             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-4-22 21:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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