On Fri, Apr 8, 2011 at 9:23 PM, Robert Love <rblove_lists@comcast.net> wrote:

Using np.loadtxt I can easily read my file that has columns of time, mode, 3 float64 for position and 3 for velocity like this.

dt = dtype([('time', '|S12'), ('mode','|S3'),('rx','f8'),('ry','f8'),('rz','f8'),('vx','f8'),('vy','f8'),('vz','f8')])

data = np.loadtxt('file', dtype=dt)


I can then put the two pairs of 3 components into np.arrays and start performing the vector operations I need.

How can I read them directly into np.arrays?


dt = dtype([('time', '|S12'), ('mode','|S3'),np.array('r'), np.array('v')])

I've seen examples for nested data that create a tuple but not an array.  Any tips  appreciated.


If you do this:

>>> dt = dtype([('time', '|S12'), ('mode','|S3'), ('r','f8', 3), ('v','f8',3)])
>>> data = np.loadtxt('file', dtype=dt)

then data['r'] and data['v'] are the arrays of positions and velocities.  You can then give them more convenient names:

>>> r = data['r']
>>> v = data['v']


Warren
 
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion