python - How to force genfromtxt read csv as record array? -
i'm trying read csv following line:
raw_data = genfromtxt(datafile,delimiter='\t',dtype=none)
ok, function reads file record array when meets string data in datafile. far understand, when dtype none, file should read record array too. correct?
however, if there no string data , numeric 1 presented, function reads data ndarray.
if no, there convenient way force function read file record array?
the problem ndarray code built in order process record arrays.
upd1 in case try it, here brief solution. possibly 1 not best, @ least works:
read file csv ndarray raw_data = genfromtxt(datafile,delimiter='\t',dtype=none)
generate default names , datatypes columns:
names_=['f'+str(i) in range(raw_data.shape[1])]; names=[(name,raw_data.dtype) name in names_];
and finaly, create record array:
raw_data_as_ra = raw_data.ravel().view(names);
you use recfromcsv
, derived genfromtxt, instead:
if file looks like:
col1,col2,col3 1.1, 2.4, 3.2 4.1, 5.2, 6.3
then 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 recfromcsv
uses first row column/record names.
also, can use same input parameters genfromtxt
(e.g. delimiter
parameter). line of code might if file tab delimited:
np.recfromcsv(datafile,delimiter='\t'))
Comments
Post a Comment