ConfigParser and .ini files
Ivan Van Laningham
ivanlan at callware.com
Fri Jan 21 14:28:32 EST 2000
Hi All--
"Fred L. Drake, Jr." wrote:
>
> Ivan Van Laningham writes:
> > The spec is how Windows treats .ini files. ...
>
> That's a huge problem for those of us who don't often use Windows.
> If anyone has tried to document the format with real details, I'd love
> to get a pointer to the description.
>
I'll take a look at the windocs later today.
> > 1) Section headers of the form "[Button States]" come up as
> > ParsingError because of the whitespace. I read the regex for the
> > headers as allowing whitespace in the header, so what's going on here?
> > Here's the regex:
> >
> > __SECTCRE = re.compile(
> > r'\[' # [
> > r'(?P<header>[-\w]+)' # `-', `_' or any alphanum
> > r'\]' # ]
> > )
> >
> > Not being a regex guru (not even an acolyte), I'm probably unqualified
> > to evaluate it. What should this be to allow whitespace in section
> > names? [*lots* of .ini files have whitespace in the section names. ...]
>
> Add a space before or after the \w in the regex.
Works beautifully. Thanks.
> Are there any characters *not* allowed in the section names? Are
> section names supposed to be case-sensitive?
>
An informal survey (grepping my winnt/*.ini files) reveals:
alphanumeric, tabs and spaces, -, _, ., (, ), *, commas, { and }.
Pretty much any character that is legal in a dos/win filename appears to
be legal as a section name.
> > 2) The ParsingError lists the lines it thinks are bad at the end of the
> > run, but they come up like this on the listing:
> >
> > [line 5]: '[Button States]\012'
> > [line 23]: '[CallWare Archive]\012'
> >
> > Now, is this because ConfigParser thinks it's on UNIX, because it treats
> > everything as a UNIX file, because it is not expecting the '\r\n'
> > combination, or because it's broken?
>
> This is because the files are opened in text mode, and CR/LF is
> represented as LF internally. This is normal. The only error was
> that the space wasn't allowed in the section name, as you pointed
> out.
>
OK, thanks.
<is-it-an-ini-or-an-outi?>-ly y'rs,
Ivan;-)
----------------------------------------------
Ivan Van Laningham
Callware Technologies, Inc.
ivanlan at callware.com
ivanlan at home.com
http://www.pauahtun.org
See also:
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
----------------------------------------------
More information about the Python-list
mailing list