CSV(逗号分隔值),是一种常见的文件格式,用来存储批量一二维数据。
在numpy中,向csv文件写入数据和读出数据通过以下两个函数实现:
np.savetxt(frame,array,fmt="%.18e",delimiter=None)
a = np.arange(100).reshape(20,5) np.savetxt("a.csv",a,fmt="%d",delimiter=",") np.loadtxt(frame,dtype=np.float,delimiter=None,unpack=False)
b= np.loadtxt("a.csv",dtype=np.int,delimiter=',')在numpy中,写入和读出高维数据通过以下两个函数实现:
a.tofile(fname,sep=’’,format=’%s’)
a = np.arange(100).reshape(20,5) a.tofile("a.bat",format='%d') np.fromfile(fname,dtype=np.float,count=-1,sep=’’)
b = np.fromfile("a.bat",dtype=np.int,sep='')但是通过此过程维度信息会丢失,仅会返回一维数据,故使用时需要备份维度信息即a.shape()
可以保存维度信息,但扩展名为.npy
np.save(frame,array)
>>>a = np.arange(100).reshape((20,5)) >>>np.save("a.npy",a) np.load(fname)
>>>b = np.load("a.np")