Python 我能';t加载我刚刚保存的numpy.npy文件
要保存Python 我能';t加载我刚刚保存的numpy.npy文件,python,numpy,Python,Numpy,要保存numpy数组,我将执行以下操作: save_tokens = 'myfile.npy' token_file = open(save_tokens, 'ab') tokens = np.array([], dtype='object') line_count = 0 tokens_to_save = np.array([], dtype='object') with open(self.corpus_file) as infile: for line in infile:
numpy
数组,我将执行以下操作:
save_tokens = 'myfile.npy'
token_file = open(save_tokens, 'ab')
tokens = np.array([], dtype='object')
line_count = 0
tokens_to_save = np.array([], dtype='object')
with open(self.corpus_file) as infile:
for line in infile:
if line_count % 1000 == 0:
print("Line Count: ", line_count, '')
if save_tokens is not None:
np.save(token_file, tokens_to_save)
tokens_to_save = np.array([], dtype='object')
line_count += 1
line_tokens = pygments.lex(line + '\n', self.lexer)
for line_token in line_tokens:
tokens = np.append(tokens, line_token[1])
tokens_to_save = np.append(tokens_to_save, line_token[1])
if line_count % 10000 == 0:
print("\tToken Count: ", len(tokens))
np.save(token_file, tokens_to_save)
我可以确认它已保存,并且有一个名为myfile.npy
的文件,大小为1.8MB
当我尝试加载并阅读它时:
f = open('myfile.npy', 'rb')
self.tokens = np.load(f, allow_pickle=True)
[print(token) for token in self.tokens]
print(self.tokens)
f.close()
return self.tokens
我还尝试:
self.tokens = np.load('myfile.npy', allow_pickle=True)
[print(token) for token in self.tokens]
print(self.tokens)
return self.tokens
它打印一个空列表
[]
。它怎么可能是空的?您不需要打开文件,就可以使用np.load
和np.save来读取或输出numpy
数组
要保存,应该是以下内容:
np.save('myfile',tokens_to_save)
然后加载以下内容:
self.tokens = np.load('myfile.npy', allow_pickle=True)
编辑
不能像这样迭代保存numpy
数组。如果要以迭代方式执行,请将其保存为文本文件np.save
将覆盖您调用的文件。考虑下面的例子,
np.save('test',np.arange(0,100,10))
np.save('test',np.arange(0,200,10))
p = np.load('test.npy',)
print p
读入时的唯一输出是[0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190]
让我们对一个文件执行多个保存操作:
In [92]: ofile = open('test.npy', 'ab')
In [93]: np.save(ofile, np.array([], object))
In [94]: arr = np.array([], object)
In [95]: arr = np.append(arr, np.array([1,2,3]))
In [96]: arr
Out[96]: array([1, 2, 3], dtype=object)
In [97]: arr = np.append(arr, np.array([1,2,3]))
In [98]: arr
Out[98]: array([1, 2, 3, 1, 2, 3], dtype=object)
In [99]: np.save(ofile, arr)
In [100]: np.save(ofile, np.arange(12).reshape(3,4))
In [101]: ofile.close()
进行普通加载:
In [103]: np.load('test.npy', allow_pickle=True)
Out[103]: array(['✪'], dtype=object)
看起来我得到了原始的[]
数组,但它的内容是什么
而是打开文件,并尝试重复加载:
In [107]: f = open('test.npy', 'rb')
In [108]: np.load(f, allow_pickle=True)
Out[108]: array(['✪'], dtype=object) # one
In [109]: np.load(f, allow_pickle=True)
Out[109]: array([], dtype=object) # two
In [110]: np.load(f, allow_pickle=True)
Out[110]: array([1, 2, 3, 1, 2, 3], dtype=object) # three
In [111]: np.load(f, allow_pickle=True) # four
Out[111]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
In [112]: np.load(f, allow_pickle=True)
---------------------------------------------------------------------------
EOFError
因此,可以将多个数组保存并加载到一个文件中,但这并不是它的初衷np.savez
用于保存多个文件。保存对象数据类型数组可能会有问题。他们的数据缓冲区有指向内存中其他位置的对象的指针。指针在保存/加载序列中无效。因此,它必须使用酸洗。当我这样做时,结果是相同的。我猜你已经尝试过了,但是你尝试过包含一个绝对路径吗?我使用的路径与中的路径相同。保存在保存数组时,还要删除打开(保存令牌)
。我需要保持打开(保存令牌)
由于我一直在不断地追加,如果您反复将np.save
添加到ab
文件中,您将保存一组数组。但是np.load
只会加载您保存的第一个数组,它显然是np.array([])
。您不应该创建np.array([],object)
数组并重复np.append
。即使它能工作,它也是低效的。使用list append创建数组列表。然后从中形成一个数组。并且只保存一次。您是否可以使用savez
更新您的答案,这样我就可以看到如何连续追加到数组(保存到磁盘)?