ConfigParser 部分下的相同选项名称

问题描述 投票:0回答:0

有没有办法在 ini 文件的同一部分下使用不同键的相同选项名称? 有一个文本框,我想在其中发布模组和地图的全部信息。 MapListConfig 和 CF_ModIds.

任何人都希望能够在另一个框架上使用 bEnableAntiCheatForServer 和 WhitelistedAdmins 进行设置。

ini文件:

[/Script/ProjectX.ProjectXGameInstance]
bEnableAntiCheatForServer=true
MapListConfig=/Smaller_Archipelago_TDM/LI_Smaller_Archipelago_TDM.LI_Smaller_Archipelago_TDM_C
MapListConfig=/Smaller_Canyon_TDM/LI_Smaller_Canyon_TDM.LI_Smaller_Canyon_TDM_C
MapListConfig=/Smaller_Valley_TDM/LI_Smaller_Valley_TDM.LI_Smaller_Valley_TDM_C
MutatorList=/fiestaOpti/fiestaOptiMutator.fiestaOptiMutator_C
CF_ModIds=825198
CF_ModIds=825520
CF_ModIds=828305
CF_ModIds=828915
WhitelistedAdmins=

我现在刚收到这个错误:

configparser.DuplicateOptionError:从“game.ini”读取时[第 40 行]:“/Script/ProjectX.ProjectXGameInstance”部分中的选项“maplistconfig”已经存在

我的一些代码:

par = configparser.ConfigParser()
par.read('game.ini')
session_name = par.get('/Script/ProjectX.ProjectXGameSession', 'servername')
session_region = par.get('/Script/ProjectX.ProjectXGameSession', 'serverregion')
session_password = par.get('/Script/ProjectX.ProjectXGameSession', 'serverpassword')
......

        # create mods & maps frame
        self.mods_f = customtkinter.CTkFrame(self, corner_radius=0, fg_color="transparent")
        self.mods_f.grid_columnconfigure((0, 1, 2), weight=3)

        #Mods Lable
        self.mods_f.lable = customtkinter.CTkLabel(self.mods_f, text="Paste all your mods & maps in the text field", font=("TkDefaultFont", 20),)
        self.mods_f.lable.grid(row=0, column=1, padx=(0, 0), pady=(20, 0), sticky="nsew")

        #Mods textbox
        self.mods_f.textbox = customtkinter.CTkTextbox(self.mods_f, width=250)
        self.mods_f.textbox.grid(row=1, column=0, padx=(20, 20), pady=(30, 0), sticky="nsew", columnspan=3)
        self.mods_f.textbox.insert(10, instance_mods)

        # INI save button def
        def save_mods():
            par.read("game.ini")
            par.set('/Script/ProjectX.ProjectXGameInstance', 'CF_ModIds', self.mods_f.textbox.get())
            with open('game.ini', 'w') as configfile:
                par.write(configfile)

        self.mods_f.main_button_1 = customtkinter.CTkButton(master=self.mods_f, text="Save", fg_color="transparent", border_width=2, text_color=("gray10", "#DCE4EE"), command=save_mods)
        self.mods_f.main_button_1.grid(row=50, column=1, padx=(20, 0), pady=(20, 20), sticky="nsew")

一直在网上搜索,但没有找到解决办法。

python configparser customtkinter
© www.soinside.com 2019 - 2024. All rights reserved.