|
用numpy 来实现数据的存储。
- import numpy as np
- import os
- os.chdir('./')
- a = np.arange(0,12)
- np.save('a.npy',a.reshape(3,4))
- c = np.load('a.npy')
- print(c)
复制代码
- import numpy as np
- import os
- os.chdir('./')
- a = np.arange(0,12)
- a.reshape(3,4)
- a.tofile('a.bin')
- b = np.fromfile('a.bin',dtype=np.int64)
- c = b.reshape(3,4)
- print(c)
复制代码
- import numpy as np
- import os
- os.chdir('./')
- a = np.arange(0,12,0.5).reshape(4,-1)
- print a
- np.savetxt('a.txt',a)
- np.loadtxt('a.txt')
- print a
- b=a;
- np.savetxt('b.txt',b,fmt="%d",delimiter=",")
- b=np.loadtxt('b.txt',delimiter=',')
- print b
复制代码
这里可以将多个数组存在一个文件时在,用解压的程序可以得到存放的.npy数组。
- import numpy as np
- import os
- os.chdir('./')
- a = np.array([[1,2,3],[4,5,6]])
- b = np.arange(0,1.0,0.1)
- c=np.sin(b)
- np.savez('result.npz',a,b,sin_arr=c)
- r=np.load('result.npz')
- print(r['arr_0'])
- print(r['arr_1'])
- print(r['sin_arr'])
复制代码
|
|