我被咬通過以下numpy
行爲:爲什麼我應該給`savetxt`打開一個二進制文件而不是文本模式的文件?
In [234]: savetxt(open('/tmp/a.dat', 'wt'), array([1, 2, 3]))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-234-2adef92da877> in <module>()
----> 1 savetxt(open('/tmp/a.dat', 'wt'), array([1, 2, 3]))
/local/gerrit/python3.2/lib/python3.2/site-packages/numpy/lib/npyio.py in savetxt(fname, X, fmt, delimiter, newline)
1007 else:
1008 for row in X:
-> 1009 fh.write(asbytes(format % tuple(row) + newline))
1010 finally:
1011 if own_fh:
TypeError: must be str, not bytes
In [235]: savetxt(open('/tmp/a.dat', 'wb'), array([1, 2, 3]))
# success
我覺得這很奇怪。我試圖將我的陣列保存到文本文件中。那我爲什麼要打開二進制文件模式?
也許這是Numpy中的錯誤? –