Ever since Numpy 1.7.0, three parameters have been added to numpy.savetxt for exactly this purpose: header, footer and comments. So the code to do as you wanted can easily be written as:
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]-1))
numpy.savetxt("temp", a, fmt=fmt, header="SP,1,2,3", comments='')
Answer from makhlaghi on Stack OverflowEver since Numpy 1.7.0, three parameters have been added to numpy.savetxt for exactly this purpose: header, footer and comments. So the code to do as you wanted can easily be written as:
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]-1))
numpy.savetxt("temp", a, fmt=fmt, header="SP,1,2,3", comments='')
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.
Since 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]-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=",")
add option for header to savetxt (Trac #1079)
savetxt should allow multi-line headers
python - Save structured numpy array using np.savetxt with header - Stack Overflow
python numpy.savetxt header has extra character # - Stack Overflow
You could try a solution similar to this SO answer to pivot the data
dtypes = [('name', 'U32'), ('r', float),('m',float)]
a = np.zeros(5, dtype=dtypes)
b = numpy.vstack(map(list, a))
Where you map list over the recarray tuples, and then vertically stack them.
Then you can do the following...
names = [n for n, t in dtypes]
numpy.savetxt('test.csv', b, header=','.join(names), fmt=','.join(['%s']*b.shape[1]))
Below is sample code:
output = np.zeros(names.size, dtype=[('name', 'U32'), ('r', float),('m',float)])
np.savetxt("foo.csv", output, delimiter=",", header="name,r,m", fmt="%s,%f,%f", comments='')
As documented here.
The header and footer text are added as comments. If you want to change the comment identifier, pass the comments option (the default is #):
np.savetxt("foo.csv", x, delimiter=",", header="ID,AMOUNT",
fmt="%i", comments='')
As documented here.
it inserts the # because that line is a comment, and the default character for comments is the symbol #,
If you want to get rid of it, pass comments='' as option to savetxt.
numpy.savetxt('reference_vect_tool.00001.txt',riferimento,fmt='%.6f',header="reference", comments="")
I've narrowed my issue down to the following:
import numpy as np out_file = "results.txt" results = [[1,2,3,.4,"5"]] # just one row for testing format = ['%i', '%i', '%i', '%f', '%s'] np.savetxt(out_file, results, format, '\t')
I'm just trying to save 5 columns of data: 3 ints, 1 float, and a string. When I attempt to so, I receive the error:
File ".\npyio.py", line 1391, in savetxt
% (str(X.dtype), format))
TypeError: Mismatch between array dtype ('<U32') and format specifier ('%i %i %i %f %s')The code works just fine if I remove the string format and the corresponding value in the array.
This feels like one of those times that I'm just doing something really stupid, but after a couple hours of fruitless googling I think I need help.