简体   繁体   English

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

[英]Failed to interpret file %s as a pickle when loading an npy array

Hi I am a new user to python and want to import a saved npy array. 嗨,我是python的新用户,想要导入已保存的npy数组。 When attempting to load the npy array, I get the following error message. 尝试加载npy数组时,我收到以下错误消息。 Thanks in advance! 提前致谢!

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

Thanks for the help guys. 谢谢你的帮助。 I realized @Joe Kington was right. 我意识到@Joe Kington是对的。 The file was creating using the traditional python write to a file as opposed to numpy. 该文件使用传统的python写入文件创建而不是numpy。 This is what I had used (which didn't work): 这是我用过的(没用的):

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)

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM