Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delimiter of numpy.savetxt

I am trying to write a numpy array to a .txt file using numpy.savetxt. To the best 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 the delimiter hi between the values. Any ideas why this might be? My goal is to add new lines between each value.

like image 989
Doubt Avatar asked Mar 16 '13 21:03

Doubt


1 Answers

You need 2D array, axis 0 is the row, and axis 1 is the column. So I use z[None, :] to convert it to 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
like image 179
HYRY Avatar answered Oct 18 '22 18:10

HYRY