I have an array I would like to add a header for.
This is what i have now:
fmt = ",".join(["%s"] + ["%10.6e"] * (my_array.shape-1))
np.savetxt('final.csv', my_array, fmt=fmt,delimiter=",")
Note: this answer was written for an older version of numpy, relevant when the question was written. With modern numpy, makhlaghi's answer provides a more elegant solution.
numpy.savetxt can also write to file objects, you can open the file youself and write your header before the data:
import numpy a = numpy.array([[0.0,1.630000e+01,1.990000e+01,1.840000e+01], [1.0,1.630000e+01,1.990000e+01,1.840000e+01], [2.0,1.630000e+01,1.990000e+01,1.840000e+01]]) fmt = ",".join(["%s"] + ["%10.6e"] * (a.shape-1)) # numpy.savetxt, at least as of numpy 1.6.2, writes bytes # to file, which doesn't work with a file open in text mode. To # work around this deficiency, open the file in binary mode, and # write out the header as bytes. with open('final.csv', 'wb') as f: f.write(b'SP,1,2,3\n') #f.write(bytes("SP,"+lists+"\n","UTF-8")) #Used this line for a variable list of numbers numpy.savetxt(f, a, fmt=fmt, delimiter=",")