Question

I'm trying to read CSV with the following line:

raw_data = genfromtxt(datafile,delimiter='\t',dtype=None)

OK, this function reads this file into Record Array when it meets string data in the datafile. as far as I understand, when dtype is None, file should be read into Record Array too. Is that correct?

However, if there is no string data and only numeric one is presented, this function reads data into ndarray.

If no, is there a convenient way to force this function read file as record array?

The problem with ndarray is that all my code is built in order to process record arrays.

UPD1 Just in case someone will try to do it, here is a brief solution. Possibly this one is not the best, but at least it works:

Read file from csv as an ndarray raw_data = genfromtxt(datafile,delimiter='\t',dtype=None)

Generate default names and datatypes for columns:

names_=['f'+str(i) for i in range(raw_data.shape[1])];
names=[(name,raw_data.dtype) for name in names_];

And finaly, to create record array:

raw_data_as_ra = raw_data.ravel().view(names);
Was it helpful?

Solution

You could use recfromcsv, which is derived from genfromtxt, instead:

If your file looks like:

col1,col2,col3
1.1, 2.4, 3.2
4.1, 5.2, 6.3

Then do this

a = np.recfromcsv('yourfile.csv')

gives:

rec.array([(1.1, 2.4, 3.2), (4.1, 5.2, 6.3)], 
      dtype=[('col1', '<f8'), ('col2', '<f8'), ('col3', '<f8')])

Note that recfromcsv uses the first row as column/record names.

Also, you can use the same input parameters as genfromtxt (e.g. the delimiter parameter). Your line of code might look like this if your file is tab delimited:

np.recfromcsv(datafile,delimiter='\t'))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top