[Tutor] .readlines() condensing multiple lines

Liam Clarke cyresse at gmail.com
Wed Mar 23 04:15:51 CET 2005

>From the docs - 

In addition to the standard fopen() values 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.

So, try 

x = file(myFile, 'rU').readlines()

Or try:

x = file(myFile, 'rU')
for line in x:
     #do stuff

Let us know how that goes. 


Liam Clarke


Worse come to worse, you could always do - 
x = file(myFile, 'r').read()
listX = x.split('\r')

On Tue, 22 Mar 2005 17:10:43 -0800, Mike Hall
<michael.hall at critterpixstudios.com> wrote:
> Unless I'm mistaken .readlines() is supposed to return a list, where
> each index is a line from the file that was handed to it. Well I'm
> finding that it's putting more than one line of my file into a single
> list entry, and separating them with \r. Surely there's a way to have a
> one to one correlation between len(list) and the lines in the file the
> list was derived from...?
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.

More information about the Tutor mailing list