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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With