[Tutor] .readlines() condensing multiple lines
michael.hall at critterpixstudios.com
Wed Mar 23 19:35:29 CET 2005
Liam, "rU" worked like a charm. My previous syntax where the lines were
fOpen = file(f, "r")
fRead = fTmp.readlines()
In this instance the size of fRead would not correspond to my line
numbers. With fOpen = file(f, "rU") it now does. Thanks :)
On Mar 22, 2005, at 7:15 PM, Liam Clarke wrote:
> 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
>> one to one correlation between len(list) and the lines in the file the
>> list was derived from...?
>> Tutor maillist - Tutor at python.org
> '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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 2264 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20050323/6db9a553/attachment-0001.bin
More information about the Tutor