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 wrote: > On Mon, 26 Jan 2009 16:10:11 +0100, Andreas Waldenburger wrote: > > > On 26 Jan 2009 14:51:33 GMT Marc 'BlackJack' Rintsch > > wrote: > > > >> On Mon, 26 Jan 2009 12:22:18 +, Sion Arrowsmith wrote: > >> > >> > content = a.readl

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

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 escribió: On Sun, 2009-01-25 at 18:23 -0800, John Machin wrote: On Jan 26, 1:03 pm, "Gabriel Genellina" wrote: > En Sun, 25 Jan 2009 23:30:33 -0200, Tim Chase > escribió: > > I suppose if I were really smart, I'd dig a little deeper in the

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 escribió: On Sun, 2009-01-25 at 18:23 -0800, John Machin wrote: On Jan 26, 1:03 pm, "Gabriel Genellina" wrote: > En Sun, 25 Jan 2009 23:30:33 -0200, Tim Chase > escribió: > > I suppose if I were really smart, I'd dig a little deeper in the

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" > wrote: > > En Sun, 25 Jan 2009 23:30:33 -0200, Tim Chase > > escribió: > > > > > > > > > Unfortunately, a raw rstrip() eats other whitespace that may be > > > important. I frequently get tab-de

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 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 = list(a

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 -- http://mail.py

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

2009-01-26 Thread Sion Arrowsmith
Diez B. Roggisch 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 = a.readlines() (Ju

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

2009-01-26 Thread Tim Rowe
2009/1/25 Tim Chase : > (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 treats '\n' as a terminat

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 escribió: On Jan 26, 1:03 pm, "Gabriel Genellina" 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 = csv.reader(open('customer_x.txt','rb'), delimiter='\t'

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

2009-01-25 Thread John Machin
On Jan 26, 1:03 pm, "Gabriel Genellina" wrote: > En Sun, 25 Jan 2009 23:30:33 -0200, Tim Chase   > 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_

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 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 line.rstrip('\r\n').split('\t') f = file('cu

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 whitespace

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 e

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 ab

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 l

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 occurrenc

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

2009-01-25 Thread John Machin
On Jan 26, 12:54 am, Tim Chase 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 in text mode and is rem

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

2009-01-25 Thread vsoler
On 25 ene, 14:36, "Diez B. Roggisch" 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* t

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

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?