"\n" in ASCII-file

John Machin sjmachin at lexicon.net
Fri Jul 18 19:27:25 EDT 2003


Gerhard Häring <gh at ghaering.de> wrote in message news:<mailman.1058527708.11589.python-list at python.org>...
> Martin P wrote:
> > Hello,
> > 
> > for a short exercise-program I have to read the lines in an ASCII-file.
> > 
> > But every line has a new-line-feed ("\n") at the end of the line. (The
> > ASCII-file was made with Windows Notepad).
> > Is this only because I used Windows (and Notepad)?
> 
> Yes.
> 

I don't understand Gerhard's answer, given the word "only" in the OP's
question.

A file created in the manner described will actually have ASCII CR LF
(carriage return, line feed) at the end of each line. On Unix, the
usual methods of creating a text file will end lines with only LF. On
a Macintosh, only CR.

In all of the 3 above cases, lines read by the usual Python methods
will have the line terminator translated if necessary to LF i.e. "\n".

So, typical text files created on Windows AND Unix will actually have
"\n" at the end of each line, NOT only Windows. Lines read in the
usual fashions by Python from typical text files created on Windows
AND Unix AND Macintosh will (appear to) have "\n" at the end, NOT only
windows.




More information about the Python-list mailing list