Feb-16-2022, 01:21 PM
I am using the configparser module. I am having trouble understanding the
method. My code:
INI file:
As you can see from the config file, there are two options named "TextSize". When I run the above code, I get the following error:
The print statement in my class shows that the ordered dictionary is getting updated, like so:
Why is the read operation still throwing an exception? I don't understand.
1 |
ConfigParser(dict_type = ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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' ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
[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 = 4 |
1 |
configparser.DuplicateOptionError: While reading from 'ini_file.ini' [line 182 ]: option 'textsize' in section 'Menu' already exists |
1 |
MultiDict([( 'mainmenurandomizer' , [ '0' ]), ( 'optionsinfo' , [ '1' ]), ( 'randomizerqty' , [ '3' ]), ( 'textfont' , [ 'Calibri' ]), ( 'textsize4' , [ '22' ])] |