\r for newline in readlines function

Peter Otten __peter__ at web.de
Fri Sep 19 19:46:38 CEST 2003

Leeds, Mark wrote:

> I did more investigation into my previous problem
> and what happens is that my text file has \r for representing
> a new line instead
> of a \n. is there a way to tell the readlines
> function that the symbol for a newline is \r rather
> than \n ? right now the readlines function reads
> the\r as another item in the list and just puts
> everything into one big list because it
> doesn't find any new line characters.
>  thanks. i'm really stuck because
> i can't read my data right now.
>                                                 mark

Try opening the file with the "U" universal newlines flag:

f = file(filename, "rU")
print f.readlines()

I think this can cope with all three newline variants regardless of the
actual platform.


More information about the Python-list mailing list