Re: Reading a two-column file into an array?
- From: Jay Loden <python@xxxxxxxxxxxx>
- Date: Tue, 31 Jul 2007 01:10:55 -0400
Nagarajan wrote:
On Jul 31, 9:03 am, Gilles Ganault <nos...@xxxxxxxxxx> 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
.
- References:
- [2.5] Reading a two-column file into an array?
- From: Gilles Ganault
- Re: Reading a two-column file into an array?
- From: Nagarajan
- [2.5] Reading a two-column file into an array?
- Prev by Date: Re: Where do they tech Python officialy ?
- Next by Date: Re: Why no maintained wrapper to Win32?
- Previous by thread: Re: Reading a two-column file into an array?
- Next by thread: Re: Reading a two-column file into an array?
- Index(es):
Relevant Pages
|