ConfigParser and newlines
Tim Chase
python.list at tim.thechases.com
Fri May 15 14:45:58 EDT 2009
> test.cfg
> ============
> [Foo_Section]
>
> BODY = Line of text 1
>
> Continuing Line of text 1
> ============
>
> Executing the code
> ===============
> Python 2.5.1 Stackless 3.1b3 060516 (release25-maint, Mar 6 2009, 14:12:34)
> [GCC 4.3.0 20080428 (Red Hat 4.3.0-8)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> from ConfigParser import RawConfigParser
>>>> config = RawConfigParser()
>>>> config.read('test.cfg')
> ['test.cfg']
>>>> config.get('Foo_Section', 'BODY')
> 'Line of text 1\nContinuing Line of text 1'
> ===============
>
> I was expecting 'Line of text 1\n\nContinuing Line of text 1'
> ^^^^
> with 2 newlines, how can I achieve that with ConfigParser.
Not as best I can tell. From my
/usr/lib/python2.5/ConfigParser.py file, around line 441:
if line[0].isspace() and cursect is not None and optname:
value = line.strip()
if value:
cursect[optname] = "%s\n%s" % (cursect[optname], value)
That "value = line.strip()" is what's throwing away your extra
newline. Then the "if value" refuses to add the extra newline
because it was a blank line. It looks like this behavior was
intentional?
-tkc
More information about the Python-list
mailing list