import numpy as np
np.savetxt(r'A.txt', B, fmt='%d', delimiter=',')
A.txt---------存儲的txt文件名稱
B-------------要存儲的矩陣
舉例:
import numpy as np
Test = np.array([[1, 7], [5, 3], [0, 1]])
np.savetxt(r'Test.txt', Test, fmt='%d', delimiter=',') # 存儲矩陣Test
Test.txt文件中內容
# 將數據list of dict保存到txt
test = [{1: [7, 7], 5: [3], 0: [1, 4], 2: [3]}]
with open('F:\Five_view\Tr_neg.txt', 'w+') as f:
for i in range(0, len(test)):
for k, v in test[i].items():
f.write(f"{k}, {v}\n".format(k, v))
其中,F:\Five_view\Tr_neg.txt為存儲文件位置
result1 = np.array(test_data)
np.savetxt("test_data.txt", result1)
其中,test_data為tensor類型