Reputation: 1925
Is there any advantage of using a generator function to read lines from a file? I'm asking this because I saw something like this in a script:
with open(file, 'r') as f:
liter = (l.rstrip("\r\n") for l in f) # <generator object <genexpr> at 0x10617c728>
for line in liter:
Would this approach be better (or in what circumstances) than a simple block like:
with open(file, 'r') as f:
for line in f:
line = line.rstrip("\r\n")
Upvotes: 4
Views: 8326
Reputation: 140186
The first approach doesn't bring much:
It would have an interest if you could write your code in one line without the loop:
result = [x for x in (l.rstrip("\n") for l in f) if x]
(creating a list of non-empty lines, that is, and as a side note, you don't have to strip \r
from the end of line because text mode already does it, unless of course there are more than 1 \r
character, which is non-standard)
Or in a lambda
that you would use all over your code to avoid forgetting removing those damn linefeeds:
stripped_lines = lambda f : (l.rstrip("\n") for l in f)
then
for l in stripped_lines(f):
...
The second approach is clearer and simpler if you have to use a loop (if you have side-effect calls, it's recommended to avoid comprehensions and use loops instead)
Upvotes: 4