ConfigParser and .ini files

Fred L. Drake, Jr. fdrake at acm.org
Fri Jan 21 14:10:21 EST 2000


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.

 > 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.
  Are there any characters *not* allowed in the section names?  Are
section names supposed to be case-sensitive?

 > 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.


  -Fred

--
Fred L. Drake, Jr.	  <fdrake at acm.org>
Corporation for National Research Initiatives




More information about the Python-list mailing list