How to handle empty values in config files with ConfigParser?

Viewed 29913

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=
5 Answers

Why not comment out the rb option like so:

[section]
person=name
id=000
; rb=

and then use this awesome oneliner:

rb = parser.getint('section', 'rb') if parser.has_option('section', 'rb') else None
Related