Re: Reading a two-column file into an array?

2007-08-02 Thread Gilles Ganault
On Tue, 31 Jul 2007 08:41:45 -0700, [EMAIL PROTECTED] (Alex Martelli)
wrote:
That's what 2.5's with statement is all about...:

Thanks everyone. Python power :-)

from __future__ import with_statement
import csv

with open('import.csv', 'rb') as f:
for item in list(csv.reader(f, delimiter='\t')):
print item[0] + , + item[1]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading a two-column file into an array?

2007-07-31 Thread Marc 'BlackJack' Rintsch
On Mon, 30 Jul 2007 21:57:17 -0700, Nagarajan wrote:

 a = []
 import csv
 reader = csv.reader(open(filename, r), delimiter='\t' )
 for row in reader:
 a.append( row )

I would keep a reference to the file to close it properly and the loop can
be replaced by a call to `list()`:

import csv

def main():
data_file = open('filename', 'rb')
a = list(csv.reader(data_file, delimiter='\t'))
data_file.close()

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading a two-column file into an array?

2007-07-31 Thread Alex Martelli
Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote:

 On Mon, 30 Jul 2007 21:57:17 -0700, Nagarajan wrote:
 
  a = []
  import csv
  reader = csv.reader(open(filename, r), delimiter='\t' )
  for row in reader:
  a.append( row )
 
 I would keep a reference to the file to close it properly and the loop can
 be replaced by a call to `list()`:
 
 import csv
 
 def main():
 data_file = open('filename', 'rb')
 a = list(csv.reader(data_file, delimiter='\t'))
 data_file.close()

That's what 2.5's with statement is all about...:

from __future__ import with_statement

def main():
with open('filename', 'rb') as f:
return list(csv.reader(f, delimiter='\t'))


Alex
-- 
http://mail.python.org/mailman/listinfo/python-list


[2.5] Reading a two-column file into an array?

2007-07-30 Thread Gilles Ganault
Hello

I'm sure there's a much easier way to read a two-column, CSV file into
an array, but I haven't found it in Google.

Should I use the Array module instead?

=
a = []
i = 0

#itemTABitemCRLF
p = re.compile(^(.+)\t(.+)$)

for line in textlines:
m = p.search(line)
if m:
a[i,0] = m.group(1)
a[i,1] = m.group(2)
i = i + 1

for i in a.count:
for j in 2:
print a[i,j]
===

Thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [2.5] Reading a two-column file into an array?

2007-07-30 Thread Erik Max Francis
Gilles Ganault wrote:

 I'm sure there's a much easier way to read a two-column, CSV file into
 an array, but I haven't found it in Google.
 
 Should I use the Array module instead?

The csv module?  Or just .rstrip and .split?

-- 
Erik Max Francis  [EMAIL PROTECTED]  http://www.alcyone.com/max/
  San Jose, CA, USA  37 20 N 121 53 W  AIM, Y!M erikmaxfrancis
   I get my kicks above the wasteline, sunshine
-- The American, _Chess_
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading a two-column file into an array?

2007-07-30 Thread Nagarajan
On Jul 31, 9:03 am, Gilles Ganault [EMAIL PROTECTED] wrote:
 Hello

 I'm sure there's a much easier way to read a two-column, CSV file into
 an array, but I haven't found it in Google.

 Should I use the Array module instead?

 =
 a = []
 i = 0

 #itemTABitemCRLF
 p = re.compile(^(.+)\t(.+)$)

 for line in textlines:
 m = p.search(line)
 if m:
 a[i,0] = m.group(1)
 a[i,1] = m.group(2)
 i = i + 1

 for i in a.count:
 for j in 2:
 print a[i,j]
 ===

 Thank you.

a = []
import csv
reader = csv.reader(open(filename, r), delimiter='\t' )
for row in reader:
a.append( row )


I don't think you can have multidimensional arrays.
Did you test you program? It did not work for me.
I think mine would suit your requirements as the output is a list of
lists.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading a two-column file into an array?

2007-07-30 Thread Jay Loden
Nagarajan wrote:
 On Jul 31, 9:03 am, Gilles Ganault [EMAIL PROTECTED] wrote:
 Hello

 I'm sure there's a much easier way to read a two-column, CSV file into
 an array, but I haven't found it in Google.

 Should I use the Array module instead?

[...snip]

 a = []
 import csv
 reader = csv.reader(open(filename, r), delimiter='\t' )
 for row in reader:
 a.append( row )
 
 
 I don't think you can have multidimensional arrays.
 Did you test you program? It did not work for me.
 I think mine would suit your requirements as the output is a list of
 lists.

I am similarly confused as to the nature of the original request, but for 
completeness' sake, I went by the same assumption of building a list of lists, 
and came up with this (which does not use the csv module). Nagarajan's code is 
more concise and just as readable IMO, but here's my take anyway:

a = []
b = []
handle = open(filename, 'r')

for line in handle.xreadlines():
col1,col2 = line.split('\t')
a.append(col1)
b.append(col2)

columns = [a, b]

-Jay
-- 
http://mail.python.org/mailman/listinfo/python-list