Python 3 numpy.load()直到文件结尾 [英] Python 3 numpy.load() until End of File
本文介绍了Python 3 numpy.load()直到文件结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我正在生成需要序列化的随机数的数组
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)
现在我如何确切地知道需要多少次ncode.load()才能取回所有数组? np.load()
最终将引发异常,所以我想到了
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.
推荐答案
您尝试了以下方法吗?
data = np.load('out.npy')
参考: https://numpy.org/doc/stable/reference/generation/numpy.load.html
这篇关于Python 3 numpy.load()直到文件结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文