# Python–数组的存储和读取，避免memory error

Please refresh the page if equations are not rendered correctly.
---------------------------------------------------------------

# 一. Numpy 方法

import numpy as np
'''二维数组的保存和读取'''
Matrix=np.array([[1,2,3],[4,5,6],[7,8,9]])
Matrix.shape
[out]:(3, 3)


np.savetxt("H:/a.txt", Matrix, fmt = '%f', delimiter = ',')
M
[out]:
array([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]], dtype=float32)

np.savetxt("H:/a2.csv", Matrix, fmt = '%f', delimiter = ',')
M1=np.genfromtxt("H:/a2.csv", delimiter = ',',dtype=np.float32)
M
[out]:array([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]], dtype=float32)


np.save("H:/b.npy",Matrix)
M
[out]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])


## 1.3 .tofile()/np.fromfile()

np.fromfile 需要手动指定读出来的数据的的dtype，如果指定的格式与保存时的不一致，则读出来的就是错误的数据。
tofile函数不能保存当前数据的行列信息，不管数组的排列顺序是C语言格式的还是Fortran语言格式，统一使用C语言格式输出。

Matrix.tofile("H:/c.bin")
M1=np.fromfile("H:/c.bin",dtype=np.int32)
M2=np.fromfile("H:/c.bin",dtype=np.int64)
M3=np.fromfile("H:/c.bin",dtype=np.float32)
M4=np.fromfile("H:/c.bin",dtype=np.float64)
print(M1,M2,M3,M4)
[out]:
[1 2 3 4 5 6 7 8 9]
[ 8589934593 17179869187 25769803781 34359738375]
[1.4e-45 2.8e-45 4.2e-45 5.6e-45 7.0e-45 8.4e-45 9.8e-45 1.1e-44 1.3e-44] [4.24399158e-314 8.48798317e-314 1.27319747e-313 1.69759663e-313]


# 二. Python直接读取

while True:
if not block:
break


while True:
if not line:
break


for line in f.readlines():


## 2.4 linecache 只读取其中一行

import linecache
text=linecache.getline("H:/a.txt",1) #行数从1开始
%time text=linecache.getline("H:/a.txt",1)
text
[out]:
Wall time: 0 ns
Out[40]:
'0.000000,1.000000,2.000000,3.000000,4.000000,5.000000,6.000000,7.000000,8.000000,9.000000,10.000000,11.000000,12.000000,13.000000,14.000000,15.000000,16.000000,17.000000,18.000000,19.000000,20.000000,21.000000,22.000000,23.000000,24.000000,25.000000,26.000000,27.000000,28.000000,29.000000,30.000000,31.000000,32.000000,33.000000,34.000000,35.000000,36.000000,37.000000,38.000000,39.000000\n'

Everything not saved will be lost.