如何将字符串和ndarray顺序写入文件?

时间:2016-10-21 19:08:07

标签: python numpy io output

我有3个numpy ndarray(比如A,B,C,尺寸为2),其元素是' type是浮点数。我想按以下格式按顺序将它们写入文件:

“first:”
A

"second:"
B

"third:"
C

我尝试了以下代码并得到了#34; IndexError:元组索引超出范围"

with open("out.txt", "wb") as f:
    np.savetxt(f,"A:")
    np.savetxt(f, A, fmt="%s")
    np.savetxt(f,"B:")
    np.savetxt(f, B, fmt='%s')
    np.savetxt(f,"C:")
    np.savetxt(f, C, fmt='%s')

然后我试了

with open("out.txt", "wb") as f:
   f.write("A:\n")
   np.savetxt(f, A, fmt="%s")
   f.write("B:\n")
   np.savetxt(f, B, fmt='%s')
   f.write("C:\n")
   np.savetxt(f, C, fmt='%s')

并得到" TypeError:需要类似字节的对象,而不是' str'"

提前致谢!

0 个答案:

没有答案