Peter Perez wrote:
>
>
> Hi all,
>
> I need to read a tab-delimited file with numeric data in columns, and
> then create an array with each of the columns of the file. Although I
> am a newbie in Python, I thought it would be as easy (or easier) than
> in Matlab, but that doesn't seem to be the case. I haven't found any
> way to do that, neither in the tutorials nor the online documentation.
> So far, I only know how to do this:
>
> filename = 'ptrace.csv'
> reader = csv.reader(open(filename,'r'),dialect='excel-tab')
You should use the "rb" flag - csv likes the file opened as binary.
>
> But I don't know how to load the numeric data into an array that Python
> can manipulate. Could somebody help me please? Thank you very much for
> your kind attention. Best regards, Peter
Do you know how many columns you have in the file? And if so, does the
column count vary for each row?
Something like this would work:
col1 = []
col2 = []
col3 = []
def populateColumns(r):
col1.append(r[0])
col2.append(r[1])
col3.append(r[2])
reader = csv.reader(open(filename,'rb'),dialect='excel-tab')
for row in reader:
populateColumns(row)
Just define as many lists as you need - one list per column.
- khill