parsing a long text file for specific text

Jason Orendorff jason at
Wed Jan 30 16:40:17 CET 2002

"Jim Ragsdale" <overlord at> wrote:
> Before if I have done anything like this, I used a loop to check 
> to see if it matched a piece of text, but this takes a while.
> Is there a better way?

You could try mmap.

import mmap
import re

def search(filename, rx):
    f = open(filename, 'r+')
    mem = mmap.mmap(f.fileno(), 0)
    for match in rx.finditer(mem):

This runs significantly faster than for/xreadlines()/search(),
and 50% faster than read()/finditer() for my test file.

## Jason Orendorff

More information about the Python-list mailing list