views:

37

answers:

2

Hi.

I am trying to create a line-by-line filter in python. However, stdin.readlines() reads all lines in before starting to process, and python runs out of memory (MemoryError).

How can I have just one line in memory at a time?

The kind of code I have:

for line in sys.stdin.readlines():
    if( filter.apply( line ) ):
        print( line )

(note: I'm on 2.6)

+3  A: 
for line in sys.stdin:
    ...

Or call .readline() in a loop.

rkhayrov
+1  A: 
import sys
while 1:
    line = sys.stdin.readline()
    if not line:
        break
    if (filter.apply(line)):
        print(line)
dogbane