Re: [Tutor] .readlines() condensing multiple lines
Mike Hall wrote: On Mar 23, 2005, at 3:17 AM, Kent Johnson wrote: Anyway, Mike, it seems clear that your file has line endings in it which are not consistent with the default for your OS. If reading with universal newlines doesn't solve the problem, please let us know what OS you are running under and give more details about the data. Kent, reading universal did indeed solve my problem, but for the record I'm on OSX, and was reading from a standard plain text file. OK good. OSX uses line feed (\n) for the line separator. It sounds like your text file has mixed line endings. Mac OS 9 uses carriage return (\r) for line separator, maybe your file has some old data mixed in? Anyway, I'm glad you got it working! Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] .readlines() condensing multiple lines
On Mar 23, 2005, at 3:17 AM, Kent Johnson wrote: Anyway, Mike, it seems clear that your file has line endings in it which are not consistent with the default for your OS. If reading with universal newlines doesn't solve the problem, please let us know what OS you are running under and give more details about the data. Kent, reading universal did indeed solve my problem, but for the record I'm on OSX, and was reading from a standard plain text file. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] .readlines() condensing multiple lines
On Mar 23, 2005, at 12:53 AM, Alan Gauld wrote: 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. I'm using subEthaEdit, which will autowrap long lines, but it also displays line numbers, so there's no doubt where one begins and ends :) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] .readlines() condensing multiple lines
Liam, "rU" worked like a charm. My previous syntax where the lines were condensing was: 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. Regards, Liam Clarke PS 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 <[EMAIL PROTECTED]> 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@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. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] .readlines() condensing multiple lines
Liam Clarke wrote: Oh right, From his email, I got the impression he was getting a list like - [[abc\rdef\rghi\r]] We really need a clarification of what is in the original file and what results he is getting. My impression is that it is mixed line endings so the result of readlines is multiple strings some of which contain data from multiple lines, but it's really not clear from the OP. Anyway, Mike, it seems clear that your file has line endings in it which are not consistent with the default for your OS. If reading with universal newlines doesn't solve the problem, please let us know what OS you are running under and give more details about the data. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] .readlines() condensing multiple lines
> 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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] .readlines() condensing multiple lines
Oh right, From his email, I got the impression he was getting a list like - [[abc\rdef\rghi\r]] On Wed, 23 Mar 2005 00:12:12 -0500, Kent Johnson <[EMAIL PROTECTED]> wrote: > Liam Clarke wrote: > > Worse come to worse, you could always do - > > x = file(myFile, 'r').read() > > listX = x.split('\r') > > This will leave the \n in the strings. Reading with universal newlines is a > better solution. > > Kent > > ___ > Tutor maillist - Tutor@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. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] .readlines() condensing multiple lines
Liam Clarke wrote: Worse come to worse, you could always do - x = file(myFile, 'r').read() listX = x.split('\r') This will leave the \n in the strings. Reading with universal newlines is a better solution. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] .readlines() condensing multiple lines
>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. Regards, Liam Clarke PS 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 <[EMAIL PROTECTED]> 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@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. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor