鱼C论坛

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

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

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

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

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

x
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删除,或者是删除前面的保留后面的
自己代码如下
    def refresh_data(self, model_dict, model_file):
        self.target_dict = model_dict 
        self.target_file = model_file
        for filename in self.target_file:
            config = configparser.ConfigParser()
            config.optionxform = lambda option: option
            config.read(filename)
            for dict_key in self.target_dict.keys():
                file_name, ini_key = dict_key.split(':')
                if config.has_option(file_name, ini_key):
                    config.remove_option(file_name, ini_key)
                config.set(file_name,ini_key, str(self.target_dict[dict_key]))
            with open(filename,'w') as cfgfile:
                config.write(cfgfile)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-11-7 09:41:35 | 显示全部楼层
问题:如何在Python中处理INI文件中同一个section下重复的key?

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

1. 保留后面的重复key并删除前面的重复key:
def remove_duplicate_keys(config, section):    # 获取当前section的所有option    options = config.options(section)    seen = set()  # 用于记录已经出现过的key    for option in options[::-1]:  # 倒序遍历options        if option in seen:            config.remove_option(section, option)  # 删除重复的key        else:            seen.add(option)  # 将未出现过的key加入seen集合
在你的代码中,你可以在读取INI文件后,对每个section调用 remove_duplicate_keys 函数来处理重复的key。具体调用方法如下:
for filename in self.target_file:    config = configparser.ConfigParser()    config.optionxform = lambda option: option    config.read(filename)    for section in config.sections():        remove_duplicate_keys(config, section)  # 处理重复的key        for dict_key in self.target_dict.keys():            file_name, ini_key = dict_key.split(':')            if config.has_option(file_name, ini_key):                config.remove_option(file_name, ini_key)            config.set(file_name, ini_key, str(self.target_dict[dict_key]))    with open(filename, 'w') as cfgfile:        config.write(cfgfile)
通过以上修改,你的代码应该能够正确处理INI文件中同一个section下的重复key了。

球一个最佳答案谢谢啦!这对我非常重要!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

下面是修正后的代码:
import configparser

def refresh_data(model_dict, model_file):
    target_dict = model_dict
    target_file = model_file

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

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

        with open(filename, 'w') as cfgfile:
            config.write(cfgfile)

希望这可以帮助到你!如果你有任何其他问题,请随时问我。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

#修改为
                        if (self._strict and
                            (sectname, optname) in elements_added):
                        #    raise DuplicateOptionError(sectname, optname,                                                                                      
                        #                               fpname, lineno)
                            self.remove_option(sectname, optname)
                        else:
                            elements_added.add((sectname, optname))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 15:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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