How can I parse tags with no value in an ini file with python configparser module?
For example, I have the following ini and I need to parse rb. In some ini files rb has integer values and on some no value at all like the example below. How can I do that with configparser without getting a valueerror? I use the getint function
[section]
person=name
id=000
rb=
Maybe use a try...except
block:
try:
value=parser.getint(section,option)
except ValueError:
value=parser.get(section,option)
For example:
import ConfigParser
filename='config'
parser=ConfigParser.SafeConfigParser()
parser.read([filename])
print(parser.sections())
# ['section']
for section in parser.sections():
print(parser.options(section))
# ['id', 'rb', 'person']
for option in parser.options(section):
try:
value=parser.getint(section,option)
except ValueError:
value=parser.get(section,option)
print(option,value,type(value))
# ('id', 0, <type 'int'>)
# ('rb', '', <type 'str'>)
# ('person', 'name', <type 'str'>)
print(parser.items('section'))
# [('id', '000'), ('rb', ''), ('person', 'name')]