python dictionary from config parser if value and "%(" in value: argument of type 'int' is not iterable

StackOverflow https://stackoverflow.com/questions/21484716

  •  05-10-2022
  •  | 
  •  

Domanda

Getting this error in as random, sometimes appear, sometimes not:

for i in range(1,32):
    sezione = "GRP"+str(i)
    dizionarioGRP = dict(config.items(sezione))
    print int(dizionarioGRP['r'])

and this is the error

  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/home/pi/gigi.py", line 436, in run
    self.esegui()
  File "/home/pi/gigi.py", line 307, in esegui
    L.gruppo(idgruppo)
  File "/home/pi/gigi1.py", line 282, in gruppo
    dizionarioGRP = dict(config.items(sezione))
  File "/usr/lib/python2.7/ConfigParser.py", line 655, in items
    for option in options]
  File "/usr/lib/python2.7/ConfigParser.py", line 663, in _interpolate
    if value and "%(" in value:
TypeError: argument of type 'int' is not iterable

why it is referring as int, if I've converted it in a string?

È stato utile?

Soluzione

Somewhere in your code you're doing this:

config.set('something', 'something', 0) # or some integer

Or optionally you're passing in a non-string value in your ConfigParser constructor dict.

This is subtly against what the docs say:

If the given section exists, set the given option to the specified value; otherwise raise NoSectionError. While it is possible to use RawConfigParser (or ConfigParser with raw parameters set to true) for internal storage of non-string values, full functionality (including interpolation and output to files) can only be achieved using string values.

So whenever you call ConfigParser.set, make sure you wrap what you're setting in str. Otherwise it throws an error when it runs through all the values in _interpolate. (I consider some part of this behavior a bug, but it's probably kept as-is for backwards compatibility. Or something.)

Another good thing to do is use SafeConfigParser which will throw a TypeError if you try to set a non-string value, so you know this won't happen again.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top