[英]How to write multiple matrices to a text file in python?
I have a for loop creating a matrix every time and I need to write all the matrices to a text file 我每次都有一个for循环创建矩阵,我需要将所有矩阵写入文本文件
I used np.savetxt()in the for loop, but in the end, the textfile only shows the last matrix I created. 我在for循环中使用了np.savetxt(),但最后,文本文件仅显示了我创建的最后一个矩阵。
Does anyone know what happened? 有人知道发生了什么吗?
file = open("newfile.txt", "w")
for i in range (0,5):
matrix = numpy.zeros((5, 5))
np.savetxt(file, matrix)
file.close()
You need use ba
other than w
when you append a numpy array into a csv file. 将numpy数组附加到csv文件时,需要使用
w
以外的ba
。 b
is binary mode Without b
your code will have type error. b
是二进制模式如果没有b
您的代码将具有类型错误。 Tested on python3.5 在python3.5上测试
您必须以追加模式打开文件:
file = open("newfile.txt", "a")
标志“ w”导致文件被忽略,您应该使用“ a”进行追加。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.