parsing a long text file for specific text

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


"Jim Ragsdale" <overlord at netdoor.com> 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):
        print match.group(0)
    mem.close()
    f.close()

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

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list