简体   繁体   English

pickle.load()填充文本文件的EOFerror

[英]pickle.load() EOFerror with filled txt file

I have a small piece of code in my program that imports a dict from a text file 我的程序中有一小段代码可以从文本文件中导入字典
It gives me the following error: 它给了我以下错误:

Traceback (most recent call last):
  File "C:\Users\S.Latil\Desktop\Python\HomeWork Full\HomeWork\test.py", line 3, in <module>
    Lesson = pickle.load(Li)
EOFError

My code is: 我的代码是:

import pickle

with open('LessonOut.txt', 'rb') as Li:
 Lesson = pickle.load(Li)

print(Lesson)

In the file LessonOut.txt i have this string: 在文件LessonOut.txt中,我有以下字符串:

€}q X   helloqX   worldqs.

I don't understand why it gives me this error, I haven't seen a fix in other threads. 我不明白为什么它会给我这个错误,我还没有看到其他线程中的修复程序。
What it should print is {'hello','world'} 它应该打印的是{'hello','world'}
If anyone could explain why this error occurs and how to fix it it would be great 如果有人能解释为什么会发生此错误以及如何解决它,那就太好了

I found the error to the problem actually, it's pretty simple. 我实际上发现了问题的错误,这很简单。
What happened was that i copyed and pasted the content of a pickle output file into my txt and because pickle doesn't use only ascii characters it didn't copy properly. 发生的事情是我将一个pickle输出文件的内容复制并粘贴到了txt中,并且因为pickle不仅仅使用ascii字符,所以不能正确复制。 All i did was redump the dict onto the file and it worked. 我所做的就是将字典还原到文件上,并且可以正常工作。
Hope this helped someone. 希望这对某人有所帮助。

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

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