Irregular last line in a text file, was Re: Regular expressions
Ian Kelly
ian.g.kelly at gmail.com
Tue Nov 3 13:33:55 EST 2015
On Tue, Nov 3, 2015 at 9:56 AM, Tim Chase <python.list at tim.thechases.com> 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)
What would happen if you read a file opened like this without
iterating over lines?
More information about the Python-list
mailing list