加载npy数组时,无法将文件%s解释为pickle

时间:2013-11-05 16:30:53

标签: python arrays numpy load

您好我是python的新用户,想要导入已保存的npy数组。尝试加载npy数组时,我收到以下错误消息。提前谢谢!

import numpy as np

A = np.load('C:/Final Runs/lineTank.npy')

我收到这些错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 384, in load
    "Failed to interpret file %s as a pickle" % repr(file))
IOError: Failed to interpret file 'C:/Final Runs/lineTank.npy' as a pickle

2 个答案:

答案 0 :(得分:3)

感谢帮助人员。我意识到@Joe Kington是对的。该文件使用传统的python写入文件创建而不是numpy。这是我用过的(没用)(

f = open(Filename, "w")
try:
    f.write(a)
finally:
    f.close()

而不是使用numpy的保存,这有效:

import numpy as np
np.save(Filename, a)
a = np.load(Filename)

答案 1 :(得分:1)

您可能需要先将文件加载到字符串中,然后将numpy.load()加载到字符串中。

类似的东西:

with f as file.open(filename):
  foo = f.readlines()
bar = numpy.load(foo)