Iteration on file reading
Andrew Dalke
adalke at mindspring.com
Sat Oct 4 17:43:38 EDT 2003
Alex:
> There is a difference in behavior: the readline method
> returns a line WITH a trailing \n, which then gets
> printed, giving a "double-spaced" effect. Sure, you
> can strip the \n in the loop body, ....
Quite true.
As it turns out, the OP wanted to know about
for line in sys.stdin:
The post to which I replied changed the spec to
remove the newline, but the main point was to
use a generator ... which could if desired to extra
work to get rid of the "\n". It could just have
easily converted everything to uppercase or done
rot13 conversion on the text.
My reply meant to point out that the iter builtin
can be used to turn a "function returns the next
object each time it's called and a sentinel when
it's done" into an iterable. I just left out the extra
work his code did since it wasn't needed by the OP.
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list