[Numpy-discussion] data type specification when using numpy.genfromtxt

Derek Homeier derek@astro.physik.uni-goettingen...
Sun Jun 26 14:27:59 CDT 2011


On 26.06.2011, at 8:48PM, Chao YUE wrote:

> I want to read a csv file with many (49) columns, the first column is string and remaning can be float. 
> how can I avoid type in like
> 
> data=numpy.genfromtxt('data.csv',delimiter=';',names=True, dtype=(S10, float, float, ......))
> 
> Can I just specify the  type of first cloumn is tring and the remaing float? how can I do that? 

Simply use 'dtype=None' to let genfromtxt automatically determine the type (it is perhaps a bit confusing that this is not the default - maybe it should be repeated in the docstring for clarity that the default is for dtype is 'float'...). 
Also, a shorter way of typing the dtype above (e.g. in case some columns would be auto-detected as int) would be 
['S10'] + [ float for n in range(48) ]

HTH,
							Derek



More information about the NumPy-Discussion mailing list