简体   繁体   English

无法加载包含腌制数据的文件 - Python.npy I/O

[英]Cannot load file containing pickled data - Python .npy I/O

I am trying to save a dataframe and a matrix as.npy files with np.save() and then read them using np.load() but I get the following error:我正在尝试使用 np.save() 保存一个 dataframe 和一个矩阵 as.npy 文件,然后使用 np.load() 读取它们,但出现以下错误:

  File "/Users/sofiafarina/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line 457, in load
    raise ValueError("Cannot load file containing pickled data "

ValueError: Cannot load file containing pickled data when allow_pickle=False

Even if I write allow_pickle=True I get an error:即使我写 allow_pickle=True 我也会收到错误消息:

  File "/Users/sofiafarina/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line 463, in load
    "Failed to interpret file %s as a pickle" % repr(file))

OSError: Failed to interpret file 'finaldf_p_85_12.npy' as a pickle

So how could I save a df from a python script and then load it in another one?那么我如何从 python 脚本中保存一个 df,然后将其加载到另一个脚本中呢? Should I use other functions?我应该使用其他功能吗? Thank you!谢谢!

I am trying to save a dataframe and a matrix as .npy files with np.save() and then read them using np.load() but I get the following error:我正在尝试使用 np.save() 将数据框和矩阵保存为 .npy 文件,然后使用 np.load() 读取它们,但出现以下错误:

  File "/Users/sofiafarina/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line 457, in load
    raise ValueError("Cannot load file containing pickled data "

ValueError: Cannot load file containing pickled data when allow_pickle=False

Even if I write allow_pickle=True I get an error:即使我写了 allow_pickle=True 我也会得到一个错误:

  File "/Users/sofiafarina/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line 463, in load
    "Failed to interpret file %s as a pickle" % repr(file))

OSError: Failed to interpret file 'finaldf_p_85_12.npy' as a pickle

So how could I save a df from a python script and then load it in another one?那么我怎么能从 python 脚本中保存一个 df 然后在另一个脚本中加载它呢? Should I use other functions?我应该使用其他功能吗? Thank you!谢谢!

I am trying to save a dataframe and a matrix as .npy files with np.save() and then read them using np.load() but I get the following error:我正在尝试使用 np.save() 将数据框和矩阵保存为 .npy 文件,然后使用 np.load() 读取它们,但出现以下错误:

  File "/Users/sofiafarina/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line 457, in load
    raise ValueError("Cannot load file containing pickled data "

ValueError: Cannot load file containing pickled data when allow_pickle=False

Even if I write allow_pickle=True I get an error:即使我写了 allow_pickle=True 我也会得到一个错误:

  File "/Users/sofiafarina/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line 463, in load
    "Failed to interpret file %s as a pickle" % repr(file))

OSError: Failed to interpret file 'finaldf_p_85_12.npy' as a pickle

So how could I save a df from a python script and then load it in another one?那么我怎么能从 python 脚本中保存一个 df 然后在另一个脚本中加载它呢? Should I use other functions?我应该使用其他功能吗? Thank you!谢谢!

I am trying to save a dataframe and a matrix as .npy files with np.save() and then read them using np.load() but I get the following error:我正在尝试使用 np.save() 将数据框和矩阵保存为 .npy 文件,然后使用 np.load() 读取它们,但出现以下错误:

  File "/Users/sofiafarina/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line 457, in load
    raise ValueError("Cannot load file containing pickled data "

ValueError: Cannot load file containing pickled data when allow_pickle=False

Even if I write allow_pickle=True I get an error:即使我写了 allow_pickle=True 我也会得到一个错误:

  File "/Users/sofiafarina/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line 463, in load
    "Failed to interpret file %s as a pickle" % repr(file))

OSError: Failed to interpret file 'finaldf_p_85_12.npy' as a pickle

So how could I save a df from a python script and then load it in another one?那么我怎么能从 python 脚本中保存一个 df 然后在另一个脚本中加载它呢? Should I use other functions?我应该使用其他功能吗? Thank you!谢谢!

I am trying to save a dataframe and a matrix as .npy files with np.save() and then read them using np.load() but I get the following error:我正在尝试使用 np.save() 将数据框和矩阵保存为 .npy 文件,然后使用 np.load() 读取它们,但出现以下错误:

  File "/Users/sofiafarina/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line 457, in load
    raise ValueError("Cannot load file containing pickled data "

ValueError: Cannot load file containing pickled data when allow_pickle=False

Even if I write allow_pickle=True I get an error:即使我写了 allow_pickle=True 我也会得到一个错误:

  File "/Users/sofiafarina/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line 463, in load
    "Failed to interpret file %s as a pickle" % repr(file))

OSError: Failed to interpret file 'finaldf_p_85_12.npy' as a pickle

So how could I save a df from a python script and then load it in another one?那么我怎么能从 python 脚本中保存一个 df 然后在另一个脚本中加载它呢? Should I use other functions?我应该使用其他功能吗? Thank you!谢谢!

I am trying to save a dataframe and a matrix as .npy files with np.save() and then read them using np.load() but I get the following error:我正在尝试使用 np.save() 将数据框和矩阵保存为 .npy 文件,然后使用 np.load() 读取它们,但出现以下错误:

  File "/Users/sofiafarina/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line 457, in load
    raise ValueError("Cannot load file containing pickled data "

ValueError: Cannot load file containing pickled data when allow_pickle=False

Even if I write allow_pickle=True I get an error:即使我写了 allow_pickle=True 我也会得到一个错误:

  File "/Users/sofiafarina/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line 463, in load
    "Failed to interpret file %s as a pickle" % repr(file))

OSError: Failed to interpret file 'finaldf_p_85_12.npy' as a pickle

So how could I save a df from a python script and then load it in another one?那么我怎么能从 python 脚本中保存一个 df 然后在另一个脚本中加载它呢? Should I use other functions?我应该使用其他功能吗? Thank you!谢谢!

只要确保文件没有损坏。

I had the same issue.我遇到过同样的问题。 Try np.loadtxt instead.请尝试np.loadtxt

I was dealing with the problem long time.我很长时间都在处理这个问题。 I have tried all of the solutions which are listed here however they all didn't work.我已经尝试了此处列出的所有解决方案,但它们都没有用。 I have tried different versions of python such as 3.7, 2.7, 3.9 and the result was same.我尝试了 python 的不同版本,例如python 3.7, 2.7, 3.9结果是一样的。

Finally I have noticed that the file with the extension .npy is corrupted so it gives out this error.最后我注意到扩展名为.npy的文件已损坏,因此它会发出此错误。 Here is the line giving the error.这是给出错误的行。

npyFile = np.load('file1.npy')

So whoever come accross the same problem first of all it would be better to check the .npy file.因此,无论谁首先遇到同样的问题,最好检查.npy文件。

I uploaded my documents to drive and I uploaded the documents from the drive.我将文件上传到驱动器,然后从驱动器上传文件。 It is solved.它被解决了。

from google.colab import drive
drive.mount("/content/drive")
label = np.load("path/labels.npy") 

my hypothesis is when I am picking npz file, some other task is writing in that npz and in that case np.load(file_path,mmap_mode='r') gives 'Cannot load file containing pickled data when allow_pickle=False'.我的假设是当我选择 npz 文件时,其他一些任务正在那个 npz 中写入,在这种情况下np.load(file_path,mmap_mode='r')给出“当 allow_pickle=False 时无法加载包含腌制数据的文件”。

So reading it after some fixed it.所以在修复它之后阅读它。

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

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