Re: Newby: how to transform text into lines of text

2009-01-26 Thread Tim Rowe
2009/1/25 Tim Chase python.l...@tim.thechases.com: (again, a malformed text-file with no terminal '\n' may cause it to be absent from the last line) Ahem. That may be malformed for some specific file specification, but it is only malformed in general if you are using an operating system that

Re: Newby: how to transform text into lines of text

2009-01-26 Thread Sion Arrowsmith
Diez B. Roggisch de...@nospam.web.de wrote: [ ... ] Your approach of reading the full contents can be used like this: content = a.read() for line in content.split(\n): print line Or if you want the full content in memory but only ever access it on a line-by-line basis: content =

Re: Newby: how to transform text into lines of text

2009-01-26 Thread Marc 'BlackJack' Rintsch
On Mon, 26 Jan 2009 12:22:18 +, Sion Arrowsmith wrote: content = a.readlines() (Just because we can now write for line in file doesn't mean that readlines() is *totally* redundant.) But ``content = list(a)`` is shorter. :-) Ciao, Marc 'BlackJack' Rintsch --

Re: Newby: how to transform text into lines of text

2009-01-26 Thread Andreas Waldenburger
On 26 Jan 2009 14:51:33 GMT Marc 'BlackJack' Rintsch bj_...@gmx.net wrote: On Mon, 26 Jan 2009 12:22:18 +, Sion Arrowsmith wrote: content = a.readlines() (Just because we can now write for line in file doesn't mean that readlines() is *totally* redundant.) But ``content =

Re: Newby: how to transform text into lines of text

2009-01-26 Thread J. Cliff Dyer
On Sun, 2009-01-25 at 18:23 -0800, John Machin wrote: On Jan 26, 1:03 pm, Gabriel Genellina gagsl-...@yahoo.com.ar wrote: En Sun, 25 Jan 2009 23:30:33 -0200, Tim Chase python.l...@tim.thechases.com escribió: Unfortunately, a raw rstrip() eats other whitespace that may be

Re: Newby: how to transform text into lines of text

2009-01-26 Thread Gabriel Genellina
En Mon, 26 Jan 2009 13:35:39 -0200, J. Cliff Dyer j...@sdf.lonestar.org escribió: On Sun, 2009-01-25 at 18:23 -0800, John Machin wrote: On Jan 26, 1:03 pm, Gabriel Genellina gagsl-...@yahoo.com.ar wrote: En Sun, 25 Jan 2009 23:30:33 -0200, Tim Chase python.l...@tim.thechases.com escribió:

Re: Newby: how to transform text into lines of text

2009-01-26 Thread Gabriel Genellina
En Mon, 26 Jan 2009 13:35:39 -0200, J. Cliff Dyer j...@sdf.lonestar.org escribió: On Sun, 2009-01-25 at 18:23 -0800, John Machin wrote: On Jan 26, 1:03 pm, Gabriel Genellina gagsl-...@yahoo.com.ar wrote: En Sun, 25 Jan 2009 23:30:33 -0200, Tim Chase python.l...@tim.thechases.com escribió:

Re: Newby: how to transform text into lines of text

2009-01-26 Thread Marc 'BlackJack' Rintsch
On Mon, 26 Jan 2009 16:10:11 +0100, Andreas Waldenburger wrote: On 26 Jan 2009 14:51:33 GMT Marc 'BlackJack' Rintsch bj_...@gmx.net wrote: On Mon, 26 Jan 2009 12:22:18 +, Sion Arrowsmith wrote: content = a.readlines() (Just because we can now write for line in file doesn't mean

Re: Newby: how to transform text into lines of text

2009-01-26 Thread Andreas Waldenburger
On 26 Jan 2009 22:12:43 GMT Marc 'BlackJack' Rintsch bj_...@gmx.net wrote: On Mon, 26 Jan 2009 16:10:11 +0100, Andreas Waldenburger wrote: On 26 Jan 2009 14:51:33 GMT Marc 'BlackJack' Rintsch bj_...@gmx.net wrote: On Mon, 26 Jan 2009 12:22:18 +, Sion Arrowsmith wrote:

Newby: how to transform text into lines of text

2009-01-25 Thread vsoler
Hello, I'va read a text file into variable a a=open('FicheroTexto.txt','r') a.read() a contains all the lines of the text separated by '\n' characters. Now, I want to work with each line separately, without the '\n' character. How can I get variable b as a list of such lines? Thank

Re: Newby: how to transform text into lines of text

2009-01-25 Thread Diez B. Roggisch
vsoler schrieb: Hello, I'va read a text file into variable a a=open('FicheroTexto.txt','r') a.read() a contains all the lines of the text separated by '\n' characters. No, it doesn't. a.read() *returns* the contents, but you don't assign it, so it is discarded. Now, I want to

Re: Newby: how to transform text into lines of text

2009-01-25 Thread Tim Chase
The idiomatic way would be iterating over the file-object itself - which will get you the lines: with open(foo.txt) as inf: for line in inf: print line In versions of Python before the with was introduced (as in the 2.4 installations I've got at both home and work), this can

Re: Newby: how to transform text into lines of text

2009-01-25 Thread vsoler
On 25 ene, 14:36, Diez B. Roggisch de...@nospam.web.de wrote: vsoler schrieb: Hello, I'va read a text file into variable a      a=open('FicheroTexto.txt','r')      a.read() a contains all the lines of the text separated by '\n' characters. No, it doesn't. a.read() *returns* the

Re: Newby: how to transform text into lines of text

2009-01-25 Thread John Machin
On Jan 26, 12:54 am, Tim Chase python.l...@tim.thechases.com wrote: One other caveat here, line contains the newline at the end, so you might have   print line.rstrip('\r\n') to remove them. I don't understand the presence of the '\r' there. Any '\x0d' that remains after reading the file

Re: Newby: how to transform text into lines of text

2009-01-25 Thread Tim Chase
One other caveat here, line contains the newline at the end, so you might have print line.rstrip('\r\n') to remove them. I don't understand the presence of the '\r' there. Any '\x0d' that remains after reading the file in text mode and is removed by that rstrip would be a strange occurrence

Re: Newby: how to transform text into lines of text

2009-01-25 Thread John Machin
On 26/01/2009 10:34 AM, Tim Chase wrote: I believe that using the formulaic for line in file(FILENAME) iteration guarantees that each line will have at most only one '\n' and it will be at the end (again, a malformed text-file with no terminal '\n' may cause it to be absent from the last

Re: Newby: how to transform text into lines of text

2009-01-25 Thread Scott David Daniels
John Machin wrote: On 26/01/2009 10:34 AM, Tim Chase wrote: I believe that using the formulaic for line in file(FILENAME) iteration guarantees that each line will have at most only one '\n' and it will be at the end (again, a malformed text-file with no terminal '\n' may cause it to be

Re: Newby: how to transform text into lines of text

2009-01-25 Thread Steven D'Aprano
On Sun, 25 Jan 2009 17:34:18 -0600, Tim Chase wrote: Thank goodness I haven't found any of my data-sources using \n\r instead, which would require me to left-strip '\r' characters as well. Sigh. My kingdom for competency. :-/ If I recall correctly, one of the accounting systems I used eight

Re: Newby: how to transform text into lines of text

2009-01-25 Thread Tim Chase
Scott David Daniels wrote: Here's how I'd do it: with open('deheap/deheap.py', 'rU') as source: for line in source: print line.rstrip() # Avoid trailing spaces as well. This should handle \n, \r\n, and \n\r lines. Unfortunately, a raw rstrip() eats other

Re: Newby: how to transform text into lines of text

2009-01-25 Thread Gabriel Genellina
En Sun, 25 Jan 2009 23:30:33 -0200, Tim Chase python.l...@tim.thechases.com escribió: Unfortunately, a raw rstrip() eats other whitespace that may be important. I frequently get tab-delimited files, using the following pseudo-code: def clean_line(line): return

Re: Newby: how to transform text into lines of text

2009-01-25 Thread John Machin
On Jan 26, 1:03 pm, Gabriel Genellina gagsl-...@yahoo.com.ar wrote: En Sun, 25 Jan 2009 23:30:33 -0200, Tim Chase   python.l...@tim.thechases.com escribió: Unfortunately, a raw rstrip() eats other whitespace that may be   important.  I frequently get tab-delimited files, using the

Re: Newby: how to transform text into lines of text

2009-01-25 Thread Gabriel Genellina
En Mon, 26 Jan 2009 00:23:30 -0200, John Machin sjmac...@lexicon.net escribió: On Jan 26, 1:03 pm, Gabriel Genellina gagsl-...@yahoo.com.ar wrote: It's so easy that don't doing that is just inexcusable lazyness :) Your own example, written using the csv module: import csv f =