Irregular last line in a text file, was Re: Regular expressions

Peter Otten __peter__ at web.de
Tue Nov 3 10:35:22 EST 2015


Jussi Piitulainen wrote:

> Peter Otten writes:
> 
>> If a "line" is defined as a string that ends with a newline
>>
>> def ends_in_asterisk(line):
>>     return False
>>
>> would also satisfy the requirement. Lies, damned lies, and specs ;)
> 
> Even if a "line" is defined as a string that comes from reading
> something like a file with default options, a line may end in
> an asterisk.
 
Note that the last line from the file is not a line as defined by me in the 
above post ;)

>>>> [ line.endswith('*') for line in StringIO('rivi*\nrivi*\nrivi*') ]
> [False, False, True]

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"?





More information about the Python-list mailing list