Dump a NumPy array into a csv file

Dexter picture Dexter · May 21, 2011 · Viewed 750.3k times · Source

Is there a way to dump a NumPy array into a CSV file? I have a 2D NumPy array and need to dump it in human-readable format.

Answer

Jim Brissom picture Jim Brissom · May 21, 2011

numpy.savetxt saves an array to a text file.

import numpy
a = numpy.asarray([ [1,2,3], [4,5,6], [7,8,9] ])
numpy.savetxt("foo.csv", a, delimiter=",")