\r for newline in readlines function

Jules Dubois bogus at invalid.tld
Fri Sep 19 19:36:35 CEST 2003


On Fri, 19 Sep 2003 12:37:42 -0400, in article
<mailman.1063989509.22137.python-list at python.org>, 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. 

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.




More information about the Python-list mailing list