Irregular last line in a text file, was Re: Regular expressions
Tim Chase
python.list at tim.thechases.com
Tue Nov 3 14:45:47 EST 2015
On 2015-11-03 11:39, Ian Kelly wrote:
> >> because I have countless loops that look something like
> >>
> >> with open(...) as f:
> >> for line in f:
> >> line = line.rstrip('\r\n')
> >> process(line)
> >
> > What would happen if you read a file opened like this without
> > iterating over lines?
>
> I think I'd go with this:
>
> >>> def strip_newlines(iterable):
> ... for line in iterable:
> ... yield line.rstrip('\r\n')
> ...
Behind the scenes, this is what I usually end up doing, but the
effective logic is the same. I just like the notion of being able to
tell open() that I want iteratation to happen over the *content* of
the lines, ignoring the new-line delimiters.
I can't think of more than 1-2 times in my last 10+ years of
Pythoning that I've actually had potential use for the newlines,
usually on account of simply feeding the entire line back into some
filelike.write() method where I wanted the newlines in the resulting
file. But even in those cases, I seem to recall stripping off the
arbitrary newlines (LF vs. CR/LF) and then adding my own known line
delimiter.
-tkc
More information about the Python-list
mailing list