Discussion:
[Numpy-discussion] Problem while writing array with np.savetxt
Andrew Nelson
2015-09-24 07:17:12 UTC
Permalink
Dear list,
whilst trying to write an array to disk I am coming across the following.
What am I doing wrong? Surely f is a file handle?
(python 3.4.3, numpy 1.9.2)

import numpy as np
a = np.arange(10.)
with open('test.dat', 'w') as f:
np.savetxt(f, a)

---------------------------------------------------------------------------

TypeError Traceback (most recent call
last)<ipython-input-57-cf77f423517e> in <module>() 2 a =
np.arange(10.) 3 with open('test.dat', 'w') as f:----> 4
np.savetxt(f, a)
/Users/anz/Documents/Andy/programming/dev3/lib/python3.4/site-packages/numpy/lib/npyio.py
in savetxt(fname, X, fmt, delimiter, newline, header, footer,
comments) 1085 else: 1086 for row in X:-> 1087
fh.write(asbytes(format % tuple(row) + newline))
1088 if len(footer) > 0: 1089 footer =
footer.replace('\n', '\n' + comments)
TypeError: must be str, not bytes
--
_____________________________________
Dr. Andrew Nelson


_____________________________________
Eric Firing
2015-09-24 07:24:33 UTC
Permalink
Post by Andrew Nelson
Dear list,
whilst trying to write an array to disk I am coming across the
following. What am I doing wrong? Surely f is a file handle?
(python 3.4.3, numpy 1.9.2)
import numpy as np
a = np.arange(10.)
np.savetxt(f, a)
It will work if you open with 'wb'. Yes, this seems like a bug; or at
least, the anomaly should be noted in the docstring.

Eric
Post by Andrew Nelson
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-57-cf77f423517e> in<module>() 2 a = np.arange(10.)3 with open('test.dat', 'w') as f:---->
4np.savetxt(f,
a)/Users/anz/Documents/Andy/programming/dev3/lib/python3.4/site-packages/numpy/lib/npyio.py
in savetxt(fname, X, fmt, delimiter, newline, header, footer, comments)
1085 else:1086 for row in X:-> 1087fh.write(asbytes(format % tuple(row)
+ newline))1088 if len(footer) > 0:1089 footer = footer.replace('\n',
'\n' + comments)TypeError: must be str, not bytes
--
_____________________________________
Dr. Andrew Nelson
_____________________________________
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Julian Taylor
2015-09-24 08:59:19 UTC
Permalink
numpy text io is fundamentally broken in python3, there are sometimes
workarounds, but currently its probably best to stick to python2 or not
use it.
The workarounds usually just make actually fixing the functions harder.
Post by Eric Firing
Post by Andrew Nelson
Dear list,
whilst trying to write an array to disk I am coming across the
following. What am I doing wrong? Surely f is a file handle?
(python 3.4.3, numpy 1.9.2)
import numpy as np
a = np.arange(10.)
np.savetxt(f, a)
It will work if you open with 'wb'. Yes, this seems like a bug; or at
least, the anomaly should be noted in the docstring.
Eric
Post by Andrew Nelson
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-57-cf77f423517e> in<module>() 2 a = np.arange(10.)3
with open('test.dat', 'w') as f:---->
4np.savetxt(f,
a)/Users/anz/Documents/Andy/programming/dev3/lib/python3.4/site-packages/numpy/lib/npyio.py
in savetxt(fname, X, fmt, delimiter, newline, header, footer, comments)
1085 else:1086 for row in X:-> 1087fh.write(asbytes(format % tuple(row)
+ newline))1088 if len(footer) > 0:1089 footer = footer.replace('\n',
'\n' + comments)TypeError: must be str, not bytes
--
_____________________________________
Dr. Andrew Nelson
_____________________________________
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Loading...