"EOFError:Ran out of input"尝试泡菜时不断出现此错误 [英] "EOFError: Ran out of input" Keep getting this error while trying to pickle

查看:334
本文介绍了"EOFError:Ran out of input"尝试泡菜时不断出现此错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写测验程序.我正在尝试让用户有机会编写和添加自己的问题.我已经编写了询问和添加问题的函数.我想泡菜 问题列表,这样我可以在有人添加问题时自动加载新问题.

I am writing a quiz program. I am trying to give the user the opportunity to write and add their own question. I have wrote functions to ask and add questions. I am trying to pickle the list of questions so I can auto load new questions anytime somebody adds one.

这是我用来加载腌制文件的代码.

This is the code I am using to load the pickled file.

sciIn = open('sciList.txt','rb')
sci = pickle.load(sciIn)
sciIn.close()

我在添加问题的函数中有此代码.

I have this code in the function that adds questions.

sciOut = open("sciList.txt",'wb')
sci.append(dicQ)
pickle.dump(sci, sciOut)
sciOut.close()

当我运行代码时,我得到EOFError:从输入中退出,该输入指向泡菜的装载.我不确定自己在做什么错.我正在使用Python3.谢谢您的帮助!

When I run the code I get EOFError: Ran out of input which points to the loading of the pickle. I am not sure what I am doing wrong. I am using Python 3. Thanks for your help!

完整代码 http://pastebin.com/HEp0KhRA

推荐答案

我认为您可能做的是正确的酸洗,但是也许在某个时候从一个空文件中进行工作……如果您在加载时执行此操作,则可以EOF奇数错误.另外,如果您以任何方式手动编辑了文件(或未使用pickle存储数据),那么加载时也会出现EOF错误.

I think you might be doing the pickling right, but maybe working from an empty file at some point… and if you do this on the load, you can get odd EOF errors. Also, if you hand-edited the file in any way (or didn't store the data with pickle), then you can also get EOF Errors on loading.

这有效(但请注意,我将问题存储为字典).

This works (but note I'm storing questions as a dictionary).

>>> import pickle
>>> sciIn = open('sciList.txt', 'rb') # has 2 pickled dict entries already
>>> sci = pickle.load(sciIn)
>>> sci
{'what is a dog?': 'a dog', 'what kind of parrot is that?': 'a dead parrot'}
>>> sciIn.close()
>>> 
>>> sciOut = open('sciList.txt', 'wb')
>>> sci["what's your favorite colour?"] = "python"  
>>> pickle.dump(sci, sciOut)
>>> sciOut.close()

从您的代码中,您似乎在腌制一个列表而不是一个字典(因此,添加新问题的附加内容).上面的代码也应该适用于列表,您只需像在代码中所做的那样追加即可.无论如何,如果尝试使用pickle从空文件中读取对象,则会出现EOF错误-但是,如果从通过对空列表进行酸洗而创建的文件中启动代码,则应该不会出现EOF错误.

From your code, it looks like you were pickling a list instead of a dictionary (hence the append to add the new question). The above should work for a list, as well, and you'd just append as you have done in your code. Regardless, if you are trying to read from an object from an empty file with pickle, you'll get an EOF Error -- however, if you start your code from a file that was created by pickling an empty list, you shouldn't get an EOF Error.

>>> import pickle
>>> sciIn = open('sciList2.txt', 'rb') # has a pickled empty list
>>> sci = pickle.load(sciIn)
>>> sci
['what is a dog?', 'what kind of parrot is that?']
>>> sciIn.close()
>>> 
>>> sciOut = open('sciList2.txt', 'wb')
>>> sci.append("what's your favorite color?")
>>> pickle.dump(sci, sciOut)
>>> sciOut.close()

这篇关于"EOFError:Ran out of input"尝试泡菜时不断出现此错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆