python 處理
.npy文件是numpy專用的二進制文件。
np.load和np.save是讀寫磁盤數組數據的兩個主要函數,默認情況下,數組是以未壓縮的原始二進制格式保存在擴展名為.npy的文件中。
1 寫入npy文件
將數組以二進制格式保存到磁盤。
import numpy as np
a=np.arange(5)`
np.save('test.npy',a)
這樣在程序所在的文件夾就生成了一個test.npy文件
2 讀取npy文件
將test.npy文件中的數據讀出來
import numpy as np
test=np.load('/Data/word.emb.nre.npy',encoding = "latin1") #加載文件
print(type(test)) # 查看數據類型
輸出:
<class 'numpy.ndarray'>
3 存入txt文件
# 將numpy矩陣數據存入txt文件
doc = open('/Data/word.emb.nre.txt', 'a') #打開一個存儲文件,並依次寫入
for i in range (len (test)):
doc.write(str(test[i])+'\n')
doc.close()`