问题
Suppose I'm generating a random number of arrays that I need to serialize
def generator():
num = 0
while num < random.randint(0, 10):
yield np.array(range(2))
num += 1
with open('out.npy', 'wb') as f:
for item in generator():
np.save(f, item)
Now how do I know exactly how many times I have to np.load()
to get all the arrays back? np.load()
will eventually throw an exception so I came up with
with open('out.npy', 'rb') as f:
try:
while 1:
item = np.load(f)
print(item)
except:
print("EoF")
but I wonder if there is a way to detect End of File or just a better way to do this.
回答1:
Did you try the following?
data = np.load('out.npy')
reference: https://numpy.org/doc/stable/reference/generated/numpy.load.html
来源:https://stackoverflow.com/questions/62596678/python-3-numpy-load-until-end-of-file