Irregular last line in a text file, was Re: Regular expressions
Tim Chase
python.list at tim.thechases.com
Tue Nov 3 11:56:53 EST 2015
On 2015-11-03 16:35, Peter Otten wrote:
> I wish there were a way to prohibit such files. Maybe a special
> value
>
> with open(..., newline="normalize") f:
> assert all(line.endswith("\n") for line in f)
>
> to ensure that all lines end with "\n"?
Or even more valuable to me:
with open(..., newline="strip") as f:
assert all(not line.endswith(("\n", "\r")) for line in f)
because I have countless loops that look something like
with open(...) as f:
for line in f:
line = line.rstrip('\r\n')
process(line)
-tkc
More information about the Python-list
mailing list