Parse Large File - Python Tools

John La Rooy igetenoughspamalreadythanksjlr at doctor.com
Mon Aug 19 09:55:34 EDT 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
> 
> 
> File1.txt
> --------
>  This is first line
>  This is second line
>  This is NOT first line
>  This is NOT second line
>  This is fifth line 
>  This is NOT fifth line
> 
> 
> File2.txt
> --------
>  This is first line
>  This is fifth line 
>  This is second line
>  This is NOT first line
>  This is NOT second line
>  This is NOT fifth line
>  
> 
> Any pointers appreciated? 
> 
> /S
Something like this?
30MB file is no problem (reading the file will take longer than the 
re.match)

 >>> import re
 >>> s1=open("file1.txt").read()
 >>> s2=open("file2.txt").read()
 >>> r=re.compile("This is first line\n.*This is second line\n.*This is 
fifth line \n",re.S)

 >>> print r.match(s1)
<_sre.SRE_Match object at 0x818c050>
 >>> print r.match(s2)
None

John La Rooy




More information about the Python-list mailing list