[Numpy-discussion] Problem while writing array with np.savetxt

Eric Firing efiring at hawaii.edu
Thu Sep 24 03:24:33 EDT 2015


On 2015/09/23 9:17 PM, Andrew Nelson wrote:
> 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)
>

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

> ---------------------------------------------------------------------------
>
> 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
> NumPy-Discussion at scipy.org
> https://mail.scipy.org/mailman/listinfo/numpy-discussion
>




More information about the NumPy-Discussion mailing list