[Tutor] .readlines() condensing multiple lines

Alan Gauld alan.gauld at freenet.co.uk
Wed Mar 23 09:53:47 CET 2005


> 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.

\r is the carriage return marker which is used as the end of line
character on some OS. So Python sees \r as the end of the line
regardless of how your system displays the file on screen.

Typically what happens is you view the file in an application
that autrowraps long lines so it looks like multiple lines on
screen but in fact it is one long line in the file. In that
case Python will only see the single long line.

> 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...?

Should just work.
If its not the situation described above give us some more detail
and maybe a cut down example of the file content where we can see
the effect?

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list