我正在编写一个流式传输数据并将其存储到数组中的脚本,但是为了节省内存,我希望将一些数据卸载到CSV文件中。这是函数的样子:
def array_dump(file, array, targetSize, dumpSize):
if np.size(array) > targetSize:
with open(file, 'a') as csvFile:
for val in array[:dumpSize]:
csvFile.write(str(val))
csvFile.write('\n')
csvFile.close()
array = np.delete(array, np.s_[:dumpSize])
此功能成功写入CSV文件,但不会更新阵列。这是完整的Python脚本:
import csv
import numpy as np
from datetime import datetime
def array_dump(file, array, targetSize, dumpSize):
if np.size(array) > targetSize:
with open(file, 'a') as csvFile:
for val in array[:dumpSize]:
csvFile.write(str(val))
csvFile.write('\n')
csvFile.close()
#array = array[dumpSize:]
array = np.delete(array, np.s_[:dumpSize])
print(np.size(array))
currencyPair = "EUR_USD"
global fileName
now = datetime.now()
fileName = "%s-%s-%s-%s.csv" % (currencyPair, now.year, now.month, now.day)
file = open(fileName, 'a')
file.close()
global a
a = np.array([0.69126532, 0.51056279, 0.35202518, 0.13374911, 0.12558003,
0.06426236, 0.8885468 , 0.18412806, 0.96355032, 0.79755331,
0.86043666, 0.51713643, 0.43547321, 0.34684145, 0.88541084,
0.04065618, 0.25212305, 0.64831108, 0.9623507 , 0.10511934,
0.09817052, 0.15486658, 0.56793785, 0.50717163, 0.38677482,
0.63625237, 0.33494367, 0.71391515, 0.53597436, 0.44326044,
0.40208125, 0.32963563, 0.78210655, 0.23943754, 0.74833578,
0.94591554, 0.29412408, 0.71934844, 0.99787723, 0.92264647,
0.94351218, 0.19185273, 0.14808887, 0.91515938, 0.48011523,
0.33024851, 0.39953296, 0.18908139, 0.7347978 , 0.09855765,
0.06552421])
#print(a)
print(np.size(a))
array_dump(fileName, a, 50, 25)
print(np.size(a))
有什么办法可以使用这个功能来卸载,删除和更新我的数组?感谢。