[Numpy-discussion] Using loadtxt to read in mixed data types

Pierre GM pgmdevlist@gmail....
Thu Jul 2 17:56:27 CDT 2009

On Jul 2, 2009, at 6:42 PM, Peter Kelley wrote:

> Hey Everyone,
> I am reading in a file of columns with mixed data types, and the  
> number of columns can vary and their format is inputted by the user.  
> So I came up with this:
> dt=dtype({'names': [x for x in event_fields], 'formats': [b for b in  
> event_format]})
> eventArray = loadtxt(behEventFile,dt)
> where event_format is ['long', 'int', 'int', 'int', 'int', 'int',  
> 'str', 'str', 'int', 'int', 'int', 'int', 'int', 'int', 'int', 'int']
> I get TypeError: data type not understood, and I think it is because  
> the event format is a list of strings not data types. Does anyone  
> have know how to convert the list of strings into the data types for  
> dtype.

What about
'formats':[eval(b) for b in event_format]

Should it fail, try something like:
dtype([(x,eval(b)) for (x,b) in zip(event_fields, event_format)])

At least you force dtype to have the same nb of names & formats.

> -Peter
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

More information about the Numpy-discussion mailing list