You can wrap stdin to remove newlines; if you can break all trailing spaces (usually good) then this is simple:
for name in map(str.rstrip, sys.stdin): ...
You're on Py3, so it works as it is; if you are on Py2 you need to add an import, from future_builtins import map , so you get a lazy map based generator (which gives the lines as they are requested, and does not throw stdin until it ends, and then returns a list all lines).
If you need to limit newlines, a generator expression can do this:
for name in (line.rstrip("\r\n") for line in sys.stdin): ...
or with import to allow map push work to level C for (slightly) faster code (a question from 30 to several nanoseconds per line is faster than the xpr gene, but still 40 ns per line is slower than the parameter with no arguments at the top of this answer):
from operator import methodcaller for name in map(methodcaller('rstrip', '\r\n'), sys.stdin): ...
Like the first solution, on Py2, be sure to get a map from future_builtins .
source share