[英]numpy.savetxt will overwrite the original file
例如,在下面的代碼中,B 將覆蓋 A 的數據,而 C 將覆蓋 B 的數據。
A = tf.constant([[1, 2, 3, 4]], tf.float32)
B = tf.constant([[10, 20, 30, 40]], tf.float32)
C = tf.constant([[11, 22, 33, 44]], tf.float32)
np.savetxt("foo.csv", A, fmt="%d")
np.savetxt("foo.csv", B, fmt="%d")
np.savetxt("foo.csv", C, fmt="%d")
我希望每次運行時將數據直接添加到下一行。
每次使用文件名都會新創建文件 - 通過提供文件句柄,您可以更好地控制文件生命周期:
import numpy as np
A = np.array([[1, 2, 3, 4]], np.float32)
B = np.array([[10, 20, 30, 40]], np.float32)
C = np.array([[100, 200, 300, 400]], np.float32)
# open by creating new file and append to it
with open("foo.csv","w") as f:
np.savetxt(f, A, fmt="%d")
np.savetxt(f, B, fmt="%d")
# reopen and append to it
with open("foo.csv","a") as f:
np.savetxt(f, C, fmt="%d")
print(open("foo.csv").read())
輸出:
1 2 3 4
10 20 30 40
100 200 300 400
獨庫: np.savetxt
numpy.savetxt (fname, X, fmt='%.18e', delimiter='', newline='n', header='', footer='', comments='#', encoding=None)
fname:文件名或文件句柄
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.