Irregular last line in a text file, was Re: Regular expressions
Peter Otten
__peter__ at web.de
Tue Nov 3 12:44:36 EST 2015
Tim Chase wrote:
> 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)
Indeed. It's obvious now you're saying it...
More information about the Python-list
mailing list