Irregular last line in a text file, was Re: Regular expressions
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Tue Nov 3 22:39:04 EST 2015
On Wednesday 04 November 2015 03:56, Tim Chase wrote:
> Or even more valuable to me:
>
> with open(..., newline="strip") as f:
> assert all(not line.endswith(("\n", "\r")) for line in f)
# Works only on Windows text files.
def chomp(lines):
for line in lines:
yield line.rstrip('\r\n')
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): ...
--
Steve
More information about the Python-list
mailing list