what is the easiest way to parse a file

Peter Scott sketerpot at chase3000.com
Wed Sep 3 12:22:48 EDT 2003


> Is there a library in python which could be used for parsing file based upon
> various sections

I don't know what you're talking about, and I frankly doubt that
anyone else does either, but I'll hazard some guesses of vaguely
plausible things.

The standard ConfigPareser module
<http://www.python.org/doc/current/lib/module-ConfigParser.html> can
parse windows-style INI files, which are arranged in sections.

There is always the usual XML/SGML stuff; I'm not very enthusiastic
about it, but you can see the documentation.
<http://www.python.org/doc/current/lib/markup.html> If you need to
brush up on your XML, there's a nice intro at
<http://www.w3schools.com/xml/>. I say "nice" because it is the least
buzzword-crazed XML page I could think of off the top of my head.

You could also write something of your own. It shouldn't be too hard.
Perhaps you could split the file into sections with a regular
expression (try re.split(), or the split() method of a compiled
regexp), and then parse the sections individually. If you're working
with some odd format, that's probably the best solution if Google
fails you.

-Peter




More information about the Python-list mailing list