[Numpy-discussion] recarray to csv

Erin Sheldon erin.sheldon@gmail....
Fri Sep 3 08:56:25 CDT 2010


Excerpts from Guillaume Chérel's message of Fri Sep 03 09:32:02 -0400 2010:
>   Hello,
> 
> I'd like to know if there is a convenient routine to write recarrays 
> into cvs files, with the first line of the file being the name of the 
> fields.
> 
> Thanks,
> Guillaume

Yes, you can do this with the recfile package:

http://pypi.python.org/pypi/recfile/0.40
http://code.google.com/p/recfile/


import numpy
import recfile

rec = numpy.zeros(3, dtype=[('x','f4'),('y','i8')])
rec['x'] = [0,1,2]
rec['y'] = [0,1,2]


recf = recfile.Open('filename.csv', 'w', delim=',')


names = rec.dtype.names
header = ','.join(names)

recf.fobj.write(header+'\n')

recf.write(rec)

recf.close()


Erin Scott Sheldon
Brookhaven National Laboratory


More information about the NumPy-Discussion mailing list