I'm trying to save a numpy matrix (Nx3, float64) into a txt file using numpy.savetxt:
np.savetxt(f, mat, fmt='%.5f', delimiter=' ')
This line worked in python 2.7, but in python 3.5, I'm getting the following error:
TypeError: Mismatch between array dtype ('float64') and format specifier ('%.5f %.5f %.5f')
When I'm stepping into the savetxt code, the print the error (traceback.format_exc()) in the catch block (numpy.lib.npyio, line 1158), the error is completely different:
TypeError: write() argument must be str, not bytes
The line of code resulting the exception is the following:
fh.write(asbytes(format % tuple(row) + newline))
I tried to remove the asbytes, and it seems to fix this error. Is it a bug in numpy?
savetxt
opens the file inwb
mode, and thus writes everything as bytes.If instead I open the file with 'w', I get your second error:
But there's no problem with
This is on Py3.4; I don't have
numpy
installed with my 3.5 Python. But I wouldn't expect a difference.Does
work on your system? You could also try