Feb-16-2022, 01:21 PM
I am using the configparser module. I am having trouble understanding the
ConfigParser(dict_type=)method. My code:
class MultiDict(OrderedDict): def __init__(self): self.x = 0 def __setitem__(self, key, value): if key == 'textsize': key += str(self.x) OrderedDict.__setitem__(self, key, value) print(self) self.x += 1 ... config1 = cp.ConfigParser(dict_type=MultiDict) data = config1.read('ini_file.ini')INI file:
[Menu] MainMenuRandomizer=0 OptionsInfo=1 RandomizerQty=3 TextFont = Calibri TextSize = 22 TextFontStatus = Courier New TextSize = 11 TextColorNormal = 250,240,180 TextShadowColorNormal = 50,50,50 TextColorSelected = 255,255,255 TextShadowColorSelected = 50,50,50 TextColorEditing = 255,100,50 TextShadowColorEditing = 50,50,50 PositionX = 800 PositionY = 120 InfoPositionX = 480 InfoPositionY = 800 TitleColumnSize = 1000 MainItemColumnSize = 100 ItemColumnSize = 300 InfoColumnSize = 960 RowSpace = 0 RowsPerPage = 30 KeyEnable = 24 KeyUp = 200 KeyDown = 208 KeyLeft = 203 KeyRight = 205 KeyPageUp = 201 KeyPageDown = 209 KeyAdd = 78 KeySubtract = 74 KeySave = 28 KeyEditing = 156 StepValue =0.1 Decimals = 4As you can see from the config file, there are two options named "TextSize". When I run the above code, I get the following error:
configparser.DuplicateOptionError: While reading from 'ini_file.ini' [line 182]: option 'textsize' in section 'Menu' already existsThe print statement in my class shows that the ordered dictionary is getting updated, like so:
MultiDict([('mainmenurandomizer', ['0']), ('optionsinfo', ['1']), ('randomizerqty', ['3']), ('textfont', ['Calibri']), ('textsize4', ['22'])]Why is the read operation still throwing an exception? I don't understand.