On 2 Aug 2011, at 19:15, Christopher Barker wrote: > In [32]: s = numpy.array(a, dtype=tfc_dtype) > --------------------------------------------------------------------------- > TypeError Traceback (most recent > call last) > > /Users/cbarker/<ipython console> in <module>() > > TypeError: expected a readable buffer object > > OK -- I can see why you'd expect that to work. However, the trick with > structured dtypes is that the dimensionality of the inputs can be less > than obvious -- you are passing in a 1-d list of 4 numbers -- do you > want a 1-d array? or ? -- in this case, it's pretty obvious (as a > human) > what you would want -- you have a dtype with four fields, and you're > passing in four numbers, but there are so many possible combinations > that numpy doesn't try to be "smart" about it. So as a rule, you > need to > be quite specific when working with structured dtypes. > > However, the default is for numpy to map tuples to dtypes, so if you > pass in a tuple instead, it works: > > In [34]: t = tuple(a) > > In [35]: s = numpy.array(t, dtype=tfc_dtype) > > In [36]: s > Out[36]: > array((32000L, 0.789131, 0.00805999, 3882.22), > dtype=[('nps', '>u8'), ('t', '>f8'), ('e', '>f8'), ('fom', > '>f8')]) > > you were THIS close!
Thanks for the detailed discussion! BTW this works also without explicitly converting the words one by one: In [1]: l = ' 32000 7.89131E-01 8.05999E-03 3.88222E+03' In [2]: tfc_dtype = numpy.dtype([('nps', 'u8'), ('t', 'f8'), ('e', 'f8'),('fom', 'f8')]) In [3]: numpy.array(tuple(l.split()), dtype=tfc_dtype) Out[3]: array((32000L, 0.789131, 0.00805999, 3882.22), dtype=[('nps', '<u8'), ('t', '<f8'), ('e', '<f8'), ('fom', '<f8')]) Cheers, Derek _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion