简体   繁体   English

使用pickle.load()的EOFError

[英]EOFError using pickle.load()

I have the following code: 我有以下代码:

pos = [
    [('the', 'DT'), ('rabbit', 'NN'), ... ],
    [('he', 'PRP'), ('jokes', 'VBZ'), ... ],
    ...
]
with open(filename, 'w') as f:
    pickle.dump(pos, f)

Then I want to load the content: 然后我要加载内容:

with open(filename, 'r') as f:
    pos = [pickle.load(f) for i in range(the_length)]

But then I get this exception: 但是然后我得到这个异常:

...

  File "C:\Anaconda\lib\pickle.py", line 1378, in load
    return Unpickler(file).load()

  File "C:\Anaconda\lib\pickle.py", line 858, in load
    dispatch[key](self)

  File "C:\Anaconda\lib\pickle.py", line 880, in load_eof
    raise EOFError

EOFError

Actually, the first list in pos is correctly loaded ( [('the', 'DT'), ('rabbit', 'NN'), ... ] ) but the exception occurs just after. 实际上, pos的第一个列表已正确加载( [('the', 'DT'), ('rabbit', 'NN'), ... ] ),但此后才发生异常。 If I open the file, there is indeed something wrong: all the sentences are stored but the file ends with a long list like this: 如果我打开文件,则确实存在问题:存储了所有语句,但文件以如下长列表结尾:

aa(lp269
a(lp270
a(lp271
a(lp272
a(lp273
a(lp274
a(lp275
...
a(lp3531
a.

(I tried to open the file with rb and wb instead of r and w but it doesn't solve anything) (我试图用rbwb而不是rw打开文件,但它不能解决任何问题)

If you only have one dump call, you should only have one load call. 如果只有一个dump调用,则应该只有一个load调用。

with open(filename, 'r') as f:
    pos = pickle.load(f)

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

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