re.search (works)|(doesn't work) depending on for loop order

John Machin sjmachin at lexicon.net
Sat Mar 22 22:47:46 CET 2008


On Mar 23, 8:21 am, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
> On Sat, 22 Mar 2008 13:27:49 -0700, sgharvey wrote:
> > ... and by works, I mean works like I expect it to.
>
> > I'm writing my own cheesy config.ini parser because ConfigParser
> > doesn't preserve case or order of sections, or order of options w/in
> > sections.
>
> > What's confusing me is this:
> >    If I try matching every line to one pattern at a time, all the
> > patterns that are supposed to match, actually match.
> >    If I try to match every pattern to one line at a time, only one
> > pattern will match.
>
> > What am I not understanding about re.search?
>
> That has nothing to do with `re.search` but how files work.  A file has a
> "current position marker" that is advanced at each iteration to the next
> line in the file.  When it is at the end, it stays there, so you can just
> iterate *once* over an open file unless you rewind it with the `seek()`
> method.
>
> That only works on "seekable" files and it's not a good idea anyway
> because usually the files and the overhead of reading is greater than the
> time to iterate over in memory data like the patterns.
>

Unless the OP has changed the pastebin code since you read it, that's
absolutely nothing to do with his problem -- his pastebin code slurps
in the whole .ini file using file.readlines; it is not iterating over
an open file.



More information about the Python-list mailing list