\r for newline in readlines function

L

Leeds, Mark

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
 
J

Jules Dubois

On Fri, 19 Sep 2003 12:37:42 -0400, in article
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.

If you open a file in text mode, you're probably getting text as the
implementation library or operating system view it.

Under MS-DOS and derivative system (e.g., Windows), text files are stored
with CRLF as the line delimiter but are returned to the program without the
CR. Under MacOS text files use CR as the delimiter, and under Unix, LF.
is there a way to tell the readlines
function that the symbol for a newline is \r rather
than \n ?

I think this is proper behavior, and the problem lies in the combination of
file format and the operating system under which you're executing. A
simple but inefficient solution is the write your own input function to
read the file one character at a time and handle characters like CR and LF
in a manner suitable to your requirements.
 
P

Peter Otten

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.

Peter
 
D

David Eppstein

"Leeds said:
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.

Isn't this exactly what the new universal newline format is for?
<http://www.python.org/peps/pep-0278.html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top