简体   繁体   English

python:pickle.load() 引发 EOFError

[英]python: pickle.load() raising EOFError

I have a pickle file using .txt format.我有一个使用 .txt 格式的泡菜文件。 I want to load this pickle file with python 2.7.我想用python 2.7加载这个pickle文件。 The size is 438.5 MB.大小为 438.5 MB。 This is how I load data :这是我加载数据的方式:

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.我已经使用读取二进制('rb')来加载和写入二进制('wb')来写入,但它仍然有那个错误。 Do you have any idea why it's still error?你知道为什么它仍然是错误的吗? how can i solve this error?我该如何解决这个错误?

Thank you for your help....感谢您的帮助....

I encountered the same error while loading a big file dumped in highest protocol. 我在加载最高协议转储的大文件时遇到了同样的错误。

This seems to be a bug of the pickle library. 这似乎是pickle库的一个bug。 I solved it using cPickle instead. 我用cPickle解决了它。

import cPickle as pickle

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子句。

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

I had the same problem.我有同样的问题。 I was reading class objects from a .txt file.我正在从 .txt 文件中读取类对象。 This seemed to work for me:这似乎对我有用:

try: 
    <my code>
except EOFError:
     break

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

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