[Numpy-discussion] newbie: attempt at data frame

Sven Schreiber svetosch at gmx.net
Wed Jan 3 04:00:10 EST 2007


Vincent Nijs schrieb:

> If there is an easy way to read array data + variable names using the csv
> module it would be great if that could be added to cookbook/InputOutput. I
> couldn't figure out how to do it.
> 
>

Hi Vincent, of course it depends a little on how exactly your csv file
looks like, but if you just have column headers and the actual data, you
might try something like the following:

import csv
from numpy import mat	# or array if you like
file_to_read = file(filename, 'r')
read_from = csv.reader(file_to_read, skipinitialspace = True)
obslist = []
datalist = []
for line in read_from:
    obslist.append(line[0])
    datalist.append(line[1:])
file_to_read.close()
# (datalist should now be a nested list, first index rows, second
#  columns)
# (still contains the headers)
varnames = datalist.pop(0)
# now the real data
data = mat(datalist, dtype = float)

-sven



More information about the NumPy-Discussion mailing list