Irregular last line in a text file, was Re: Regular expressions

Oscar Benjamin oscar.j.benjamin at gmail.com
Wed Nov 4 05:07:14 EST 2015


On 4 November 2015 at 03:39, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
>
> Better would be this:
>
> def chomp(lines):
>     for line in lines:
>         yield line.rstrip()  # remove all trailing whitespace
>
>
> with open(...) as f:
>     for line in chomp(f): ...

with open(...) as f:
    for line in map(str.rstrip, f): ...

--
Oscar



More information about the Python-list mailing list