Python configparser will not accept keys without values

Sparc picture Sparc · Feb 29, 2012 · Viewed 10.7k times · Source

So I'm writing a script that reads from a config file, and I want to use it exactly how configparser is designed to be used as outlined here: http://docs.python.org/release/3.2.1/library/configparser.html

I am using Python 3.2.1. The script, when complete, will run on a Windows 2008 R2 machine using the same version of Python, or assuming compatibility, the latest version at the time.

#!/user/bin/env python
import configparser

config = configparser.ConfigParser()
config.read('c:\exclude.ini')
config.sections()

That works fine to read the exclude.ini file - unless I have a value without a key. Thinking I might be doing something wrong tried parsing the example listed here: http://docs.python.org/release/3.2.1/library/configparser.html#supported-ini-file-structure

It still throws the following every time:

File "C:\Python32\lib\configparser.py", line 1081, in _read
    raise e
configparser.ParsingError: Source contains parsing errors: c:\exclude.ini
    [line 20]: 'key_without_value\n'

I'm at a loss... I'm literally copy/pasting the example code from the documentation for the exact python version I'm using and it's not working as it should. I can only assume I'm missing something as I also can't really find anyone with a similar issue.

Answer

Karl Barker picture Karl Barker · Feb 29, 2012

The ConfigParser constructor has a keyword argument allow_no_value with a default value of False.

Try setting that to true, and I'm betting it'll work for you.