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

Jussi Piitulainen harvesting at makes.email.invalid
Tue Nov 3 11:42:20 EST 2015


Peter Otten writes:
> 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 ;)

Noted.

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

I'd like that. It should be the default.



More information about the Python-list mailing list