By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,715 Members | 768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,715 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

#item<TAB>item<CRLF>
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.
Jul 31 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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 && ma*@alcyone.com && 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_
Jul 31 '07 #2

P: n/a
On Jul 31, 9:03 am, Gilles Ganault <nos...@nospam.comwrote:
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

#item<TAB>item<CRLF>
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.
Jul 31 '07 #3

P: n/a
Nagarajan wrote:
On Jul 31, 9:03 am, Gilles Ganault <nos...@nospam.comwrote:
>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
Jul 31 '07 #4

P: n/a
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
Jul 31 '07 #5

P: n/a
Marc 'BlackJack' Rintsch <bj****@gmx.netwrote:
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
Jul 31 '07 #6

P: n/a
On Tue, 31 Jul 2007 08:41:45 -0700, al***@mac.com (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]
Aug 2 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.