Parse Large File - Python Tools

Thomas Guettler zopestoller at thomas-guettler.de
Mon Aug 19 14:42:39 CEST 2002


SK wrote:

> Hi All,
> 
> I want to parse a large text file( size = 30MB ). Is there any already
> available python tools ?
> 
> Say, I want to match the following pattern:-
> 
> Line "This is first line" followed by line "This is second line" 
> followed by "This is fifth line". 
> 
> Result should be match SUCCESS ONLY for File1.txt as input and NOT for
> File2.txt


Something like this?:

fd=open("file")
while 1:
    line=fd.readline()
    if not line:
        break
    if line=="one":
        one=1
    elif one and line=="two":
        print "'one' followed by 'two'"
        two=1
        one=0
    else:
        one=0
        two=0
fd.close()

You could write your line in a list (["one"], ["two"], ...) and

use a list for storing the varibles (one, two), too.

HTH

  thomas





More information about the Python-list mailing list