python:pickle.load() 引发 EOFError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37451014/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:25:13  来源:igfitidea点击:

python: pickle.load() raising EOFError

pythonpython-2.7pickle

提问by estu

I have a pickle file using .txt format. I want to load this pickle file with python 2.7. The size is 438.5 MB. This is how I load data :

我有一个使用 .txt 格式的泡菜文件。我想用python 2.7加载这个pickle文件。大小为 438.5 MB。这是我加载数据的方式:

def readpickle(path="C:/Python27/Lib/site-packages/xy/"):
with open(path+"filenamereal2.txt","rb") as f:
    model = pickle.load(f)

return model

And I get this error

我得到这个错误

    itemmodelreal=readpickle(path="C:/Users/Lab Komputasi/Documents/estu/") 
Traceback (most recent call last):
File "<ipython-input-33-265e46f74915>", line 1, in <module>
    itemmodelreal=readpickle(path="C:/Users/Lab Komputasi/Documents/estu/")

  File "<ipython-input-31-fbd3e8b9e043>", line 3, in readpickle
    model = pickle.load(f)

  File "C:\Users\Lab Komputasi\Anaconda2\lib\pickle.py", line 1384, in load
    return Unpickler(file).load()

  File "C:\Users\Lab Komputasi\Anaconda2\lib\pickle.py", line 864, in load
    dispatch[key](self)

  File "C:\Users\Lab Komputasi\Anaconda2\lib\pickle.py", line 886, in load_eof
    raise EOFError

EOFError

this is the code that i use to write pickle :

这是我用来编写泡菜的代码:

 with open("filenamereal3.txt", "wb") as f:
    pickle.dump(result, f)
f.close()

I have used read binary ('rb') to load and write binary ('wb') to write, but it's still have that error. Do you have any idea why it's still error? how can i solve this error?

我已经使用读取二进制('rb')来加载和写入二进制('wb')来写入,但它仍然有那个错误。你知道为什么它仍然是错误的吗?我该如何解决这个错误?

Thank you for your help....

感谢您的帮助....

回答by David

I encountered the same error while loading a big file dumped in highest protocol.

我在加载以最高协议转储的大文件时遇到了同样的错误。

This seems to be a bug of the pickle library. I solved it using cPickle instead.

这似乎是pickle库的一个错误。我改用 cPickle 解决了它。

import cPickle as pickle

回答by Neil Chowdhury o_O

To load data, wouldn't you want to be reading data ("rb") instead of writing data ("wb")?

要加载数据,您是否不想读取数据(“rb”)而不是写入数据(“wb”)?

Loading data should look like this:

加载数据应如下所示:

 with open("C:/Users/Lab Komputasi/Documents/estu/filenamereal1.txt", "rb") as f:
     data = pickle.load(f)

Also, using f.close() is unnecessary because you are using a with/as clause.

此外,使用 f.close() 是不必要的,因为您使用的是 with/as 子句。

回答by langlauf.io

Make sure your pickle file is not empty, e.g. if you pickle an uninitialized variable.

确保您的pickle 文件不为空,例如,如果您pickle 一个未初始化的变量。