[Tutor] parsing a "chunked" text file

Stefan Behnel stefan_ml at behnel.de
Thu Mar 18 12:54:44 CET 2010


Karim Liateni, 04.03.2010 01:23:
> Steven D'Aprano wrote:
>> def skip_blanks(lines):
>> """Remove leading and trailing whitespace, ignore blank lines."""
>> for line in lines:
>> line = line.strip()
>> if line:
>> yield line
>
> Is there a big difference to write your first functions as below because
> I am not familiar with yield keyword?
>
> def skip_blanks(lines):
> """Remove leading and trailing whitespace, ignore blank lines."""
> return [line.strip() in lines if line.strip()]

Yes, a *big* difference in the true sense of the word. Your code (assuming 
you meant to write "... for line in ..." ) evaluates the entire list 
comprehension before returning from the call. Steven's code returns a 
generator that only handles one line (or a couple of empty lines) at a 
time. So, assuming that this runs against a large file, Steven's code uses 
only a constant amount of memory, compared to the whole file in your case, 
and is likely also a lot faster than your code as it involves less looping.

Stefan



More information about the Tutor mailing list