simple text filter

Bill Anderson billa at hp.com
Wed Jun 11 23:52:28 CEST 2003


On Wed, 11 Jun 2003 13:36:52 -0700, boutros wrote:

> I need some help on a simple text filter. The problem I am having is
> when the file comes to the end it stays in the while loop and does not
> exit. I cannot figure this out. I would use a for loop with the
> readlines() but my datasets can range from 5 to 80 MB of text data.
> Here is the code I am using. Please help.
> 
> import sys, re
> 
> p1 = re.compile('ADT100')
> p2 = re.compile('AUDIT')
> p3 = re.compile('HARDWARE')
> p4 = re.compile('PACKAGES')
> p5 = re.compile('NODE')
> p6 = re.compile('DROP')
> p7 = re.compile('GRID')
> p8 = re.compile('ATAP')

If you are using Python <= 2 (?) you can instead do:

while 1:
    line = f.readline()
    if not line:
        break
    if line.find('AUDIT') > -1:
        stuff
    elif line.find('HARDWARE') > -1:
        more stuff
 

Of course, the "if not line: break" is not Python 2 specific.







More information about the Python-list mailing list