I want to open up a new text file and then save the numpy array to the file. I wrote this bit of code:
foo = np.array([1,2,3])
abc = open('file'+'_2', 'w')
np.savetxt(abc, foo, delimiter=",")
I get this error:
TypeError Traceback (most recent call last)
<ipython-input-33-fea41927952b> in <module>()
2 model = cool
3 abc = open('file'+'_2', 'w')
----> 4 np.savetxt(abc, foo, delimiter=",")
/usr/local/lib/python3.4/site-packages/numpy/lib/npyio.py in savetxt(fname, X, fmt, delimiter, newline, header, footer, comments)
1071 else:
1072 for row in X:
-> 1073 fh.write(asbytes(format % tuple(row) + newline))
1074 if len(footer) > 0:
1075 footer = footer.replace('
', '
' + comments)
TypeError: must be str, not bytes
Does anyone know whats wrong?
Additionally, I found an empty file created in the terminal called file_2, but nothing is written inside it.
EDIT: I am using Python3.4
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…