Python 加载 npy 数组时无法将文件 %s 解释为 pickle
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19793937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Failed to interpret file %s as a pickle when loading an npy array
提问by aSea
Hi I am a new user to python and want to import a saved npy array. When attempting to load the npy array, I get the following error message. Thanks in advance!
嗨,我是 python 的新用户,想导入保存的 npy 数组。尝试加载 npy 数组时,我收到以下错误消息。提前致谢!
import numpy as np
A = np.load('C:/Final Runs/lineTank.npy')
I receive these errors:
我收到这些错误:
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
回答by Nate
You might need to load the file into a string first, then numpy.load() the string.
您可能需要先将文件加载到字符串中,然后 numpy.load() 字符串。
Something like :
就像是 :
with f as file.open(filename):
foo = f.readlines()
bar = numpy.load(foo)
回答by aSea
Thanks for the help guys. I realized @Joe Kington was right. The file was creating using the traditional python write to a file as opposed to numpy. This is what I had used (which didn't work):
谢谢你们的帮助。我意识到@Joe Kington 是对的。该文件是使用传统的 python 写入文件而不是 numpy 创建的。这是我使用过的(不起作用):
f = open(Filename, "w")
try:
f.write(a)
finally:
f.close()
as opposed to using numpy's save, which works:
与使用 numpy 的保存相反,它的工作原理是:
import numpy as np
np.save(Filename, a)
a = np.load(Filename)