[Tutor] deleting CR within files

denis denis.spir at free.fr
Sat Apr 17 16:29:35 EDT 2004

----- Original Message -----
From: David Talaga <dtalaga at novodynamics.com>
To: Alan Gauld <alan.gauld at blueyonder.co.uk>; Roger Merchberger
<zmerch at 30below.com>; <tutor at python.org>
Sent: Saturday, April 17, 2004 6:57 PM
Subject: RE: [Tutor] deleting CR within files

> Actually I changed it even more since last.
> for i in in_file:
>       out_file.write(i[:])
> This does what I need with the excpetion that it dosent do it for every
> line.  It is currous because it will take off a <cr> from one line and not
> the next 2 and then from another line...  currious...

Here absracts from the standard python (2.3.3) help files (just in case).

file( filename[, mode[, bufsize]])

[...] mode may be 'U' or 'rU'. If Python is built with universal newline
support (the default) the file is opened as a text file, but lines may be
terminated by any of '\n', the Unix end-of-line convention, '\r', the
Macintosh convention or '\r\n', the Windows convention. All of these
external representations are seen as '\n' by the Python program. If Python
is built without universal newline support mode 'U' is the same as normal
text mode. Note that file objects so opened also have an attribute called
newlines which has a value of None (if no newlines have yet been seen),
'\n', '\r', '\r\n', or a tuple containing all the newline types seen.

Files support the iterator protocol. Each iteration returns the same result
as file.readline(), and iteration ends when the readline() method returns an
empty string.

newlines (file object's read-only attribute)
If Python was built with the --with-universal-newlines option (the default)
this read-only attribute exists, and for files opened in universal newline
read mode it keeps track of the types of newlines encountered while reading
the file. The values it can take are '\r', '\n', '\r\n', None (unknown, no
newlines read yet) or a tuple containing all the newline types seen, to
indicate that multiple newline conventions were encountered. For files not o
pened in universal newline read mode the value of this attribute will be

On my (windows) platform :

>>> def testNewlines():
 fich = file('d:\\essais\\test.txt','r')     # 'r' : normal newline mode
 for line in fich.readlines() : print line[:-1],
 print fich.newlines

>>> testNewlines()
a b c None

>>> def testNewlines():
 fich = file('d:\\essais\\test.txt','rU')
 for line in fich.readlines():print line[:-1],
 nl = fich.newlines
 print ord(nl[0]),ord(nl[1]),nl

>>> testNewlines()
a b c 13 10
            ^ here a non printable (square) caracter visible in Idle

I guess this control caracter is the way Idle shows '\r\n' on the screen.


More information about the Tutor mailing list