How to get rid the new line

Peter Hansen peter at engcorp.com
Thu Jun 27 16:00:22 CEST 2002


Markus von Ehr wrote:
> 
>         f = open(filename, 'r')
>         lines = f.readlines()
>         line1 = lines[0] # exemplarily for first line
>         line1 = line1[0:len(line1)-1]
> 
> or:
>         f = open(filename, 'r')
>         lines = f.readlines()
>         line1 = lines[0][0:len(line1)-1] # exemplarily for first line

I believe this is unsafe.  The final line may not be terminated with \n.

-Peter



More information about the Python-list mailing list