Reading multiline values using ConfigParser

John Krukoff jkrukoff at ltgc.com
Wed Jun 20 13:35:25 EDT 2007


> -----Original Message-----
> From: python-list-bounces+jkrukoff=ltgc.com at python.org [mailto:python-
> list-bounces+jkrukoff=ltgc.com at python.org] On Behalf Of Phoe6
> Sent: Wednesday, June 20, 2007 10:51 AM
> To: python-list at python.org
> Subject: Reading multiline values using ConfigParser
> 
> Hi,
> I have a configfile, in fact, I am providing a configfile in the
> format:
> 
> [Information]
> Name: Foo
> Author: Bar
> 
> Testcases:
> tct123
> tct124
> tct101
> 
> The last values is a multi-line.
> 
> ConfigParser is unable to recognize a multi-line value and splits out
> error.
> 
> C:\ATF-Tasks>python CreateTask.py
> Traceback (most recent call last):
>   File "CreateTask.py", line 13, in ?
>     config.read('TaskDetails.txt')
>   File "C:\Python24\lib\ConfigParser.py", line 267, in read
>     self._read(fp, filename)
>   File "C:\Python24\lib\ConfigParser.py", line 490, in _read
>     raise e
> ConfigParser.ParsingError: File contains parsing errors:
> TaskDetails.txt
>         [line 15]: 'tct123\n'
>         [line 16]: 'tct124\n'
>         [line 17]: 'tct101\n'
> 
> I am using ConfigParser in the following way:
> 
> config = ConfigParser.ConfigParser()
> config.read('TaskDetails.txt')
> config.get("Information","Testcases"):
> 
> Is there anyway, I can include multi-line value in the configfile? I
> was thinking of following option:value for a portion of the file and
> read the portion with multi-line as a normal file, but ConfigParser()
> is not allowing multi-line value itself.
> 
> Any ideas/ Suggestions? :
> 
> Ofcourse, throw away ConfigParser and use your own parser is there,
> but I would like to use that as the last option.
> 
> Thanks,
> Senthil
> 
> --
> http://mail.python.org/mailman/listinfo/python-list


Did you see the note in the docs about line continuations?

The configuration file consists of sections, led by a "[section]" header and
followed by "name: value" entries, with continuations in the style of RFC
822; "name=value" is also accepted.

Following the link to RFC 822 (http://www.faqs.org/rfcs/rfc822.html)
indicates that you can spread values out over multiple lines as long as
there is a space or tab character imeediately after the CRLF. If my
interpretation is correct (I've never tried this), then this would be a
legal multi-line value.

Testcases:
 tct123
 tct124
 tct101

It looks like you'd just get this back as the string 'tct123 tct124 tct101'
though, so you'd have to split on whitespace to get the individual values.
And, well, if you want to support whitespace in your value names, I don't
think this will work at all.

---------
John Krukoff
helot at comcast.net






More information about the Python-list mailing list