Just iterate on sys.stdin
, it will iterate on the lines.
Then, you can stack generator expressions, or use map
and filter
if you prefer. Each line that gets in will go through the pipeline, no list gets built in the process.
Here are examples of each:
import sys
stripped_lines = (line.strip() for line in sys.stdin)
lines_with_prompt = ('--> ' + line for line in stripped_lines)
uppercase_lines = map(lambda line: line.upper(), lines_with_prompt)
lines_without_dots = filter(lambda line: '.' not in line, uppercase_lines)
for line in lines_without_dots:
print(line)
And in action, in the terminal:
thierry@amd:~$ ./test.py
My first line
--> MY FIRST LINE
goes through the pipeline
--> GOES THROUGH THE PIPELINE
but not this one, filtered because of the dot.
This last one will go through
--> THIS LAST ONE WILL GO THROUGH
A shorter example with map
only, where map
will iterate on the lines of stdin
:
import sys
uppercase_lines = map(lambda line: line.upper(), sys.stdin)
for line in uppercase_lines:
print(line)
In action:
thierry@amd:~$ ./test2.py
this line will turn
THIS LINE WILL TURN
to uppercase
TO UPPERCASE