reading from stdin, while consuming no more memory than needed
-
16-09-2020 - |
Question
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)
Solution
for line in sys.stdin:
...
Or call .readline()
in a loop.
OTHER TIPS
import sys
while 1:
line = sys.stdin.readline()
if not line:
break
if (filter.apply(line)):
print(line)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow