getting rid of EOL character ?

Michael Hoffman cam.ac.uk at mh391.invalid
Fri Apr 27 09:19:52 EDT 2007


stef wrote:
> hello,
> 
> In the previous language I used,
> when reading a line by readline, the EOL character was removed.
> 
> Now I'm reading a text-file with CR+LF at the end of each line,
>    Datafile = open(filename,'r')    line = Datafile.readline()
> 
> now this gives an extra empty line
>    print line
> 
> and what I expect that should be correct, remove CR+LF,
> gives me one character too much removed
>    print line[,-2]
> 
> while this gives what I need ???
>    print line[,-1]
> 
> Is it correct that the 2 characters CR+LF are converted to 1 character ?
> Is there a more automatic way to remove the EOL from the string ?

line = line.rstrip("\r\n") should take care of it. If you leave out the 
parameter, it will strip out all whitespace at the end of the line, 
which is what I do in most cases.
-- 
Michael Hoffman



More information about the Python-list mailing list