Numpy.savetxt delimiter

I am trying to write a numpy array to a file .txtusing numpy.savetxt. As far as I can tell, the following code follows the documentation:

z = np.array([1,2,3])
np.savetxt('testdata.txt',z,delimiter='hi')

However, the output file opened with Notepad shows

1.000000000000000000e+002.000000000000000000e+003.000000000000000000e+00

without a separator hibetween values. Any idea why this might be? My goal is to add new lines between each value.

+5
source share
1 answer

You need a 2D array, the 0 axis is the row, and the 1 axis is the column. Therefore, I use z[None, :]to convert it to a 2D array:

from StringIO import StringIO
s = StringIO()
z = np.array([1,2,3])
np.savetxt(s,z[None, :],delimiter='hi')
s.getvalue()

output:

1.000000000000000000e+00hi2.000000000000000000e+00hi3.000000000000000000e+00\n
+8
source

All Articles