csv - How to read a text file with mixed data_types automatically in Python? -
i have several text files of mixed data types in different columns, want read them program recognizes each column type automatically not know column contains type.
when read numeric data used following, fails mixed datatypes.
import numpy np import csv train = np.array(list(csv.reader(open(self.source_data_file, "rb"), delimiter=','))).astype('float')
have @ numpy.genfromtxt
here : http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.genfromtxt.html
you can read files directly specifying delimiter , dtype. suppose have line in csv goes this:
10,120.3,xfghfh
you can following :
data = np.genfromtxt('input_file', dtype=none , delimiter=",") print (data)
which give :
data = array((10, 120.3, 'xfghfh'), dtype=[('column_name1', '<i4'), ('column_name2', '<f8'), ('column_name3', 's6')])
Comments
Post a Comment