- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我在尝试使用 Unpickler.load()
时遇到一个有趣的错误,这里是源代码:
open(target, 'a').close()
scores = {};
with open(target, "rb") as file:
unpickler = pickle.Unpickler(file);
scores = unpickler.load();
if not isinstance(scores, dict):
scores = {};
这是回溯:
Traceback (most recent call last):
File "G:\python\pendu\user_test.py", line 3, in <module>:
save_user_points("Magix", 30);
File "G:\python\pendu\user.py", line 22, in save_user_points:
scores = unpickler.load();
EOFError: Ran out of input
我要读取的文件是空的。我怎样才能避免出现这个错误,而是得到一个空变量?
最佳答案
这里的大多数答案都涉及如何管理 EOFError 异常,如果您不确定 pickle 对象是否为空,这非常方便。
但是,如果您对 pickle 文件为空感到惊讶,可能是因为您通过“wb”或其他可能覆盖文件的模式打开了文件名。
例如:
filename = 'cd.pkl'
with open(filename, 'wb') as f:
classification_dict = pickle.load(f)
这将覆盖 pickle 文件。您可能在使用前错误地这样做了:
...
open(filename, 'rb') as f:
然后得到 EOFError 因为前面的代码块覆盖了 cd.pkl 文件。
在 Jupyter 或控制台 (Spyder) 中工作时,我通常会在读/写代码上编写一个包装器,然后调用该包装器。这避免了常见的读写错误,并且如果您要通过您的 travails 多次读取同一个文件,可以节省一些时间
关于python - 为什么我在读取空文件时得到 "Pickle - EOFError: Ran out of input"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24791987/
从 Rails 控制台使用 MailChimp gem 时出现间歇性错误。 这是错误: Excon::Errors::SocketError: EOFError (EOFError) from /
我正在使用 tkinter 和 urllib 制作这个程序,它应该像一个下载管理器。在我几乎完成该程序后,我意识到我没有为下载定义取消按钮。在深入研究之后,我发现了多处理(在此之前我只是使用线程),显
当我尝试 chown 一个目录时,Fabric 给我一个 EOFError。只有当我从 fabric 之前运行的 python 脚本运行子进程时才会发生。 来 self 的 fabfile: ...
当我使用来自其他脚本的子进程启动我的 python3 脚本时,出现以下错误: Select the keyword preset you want to use:Traceback (most rec
编辑:我很蠢 对不起大家。我的循环不工作的原因是一个极其愚蠢的原因;我的 pickle.dump() 参数顺序错误。 操作 第一次 pickle 物体,抱歉,如果这是一个棘手的问题;看过各种其他 EO
我有一堆客户端通过 0MQ 连接到服务器。我有一个管理器队列,用于工作池与每台客户端计算机上的主进程进行通信。 在一台有 250 个工作进程的客户端机器上,我几乎立即看到一堆 EOFError。它们发
这可能会重复,但现有的答案都没有解决我的问题。 因此,我使用的是 Python 2.7,每当我尝试此操作时都会收到此错误(标题): number = int(raw_input('Number : '
这个问题在这里已经有了答案: Unable to install pygame on Python via pip (Windows 10) (6 个答案) 关闭 2 年前。 我正在使用 Windo
我正在尝试使用 pickle 为我的游戏创建一个保存文件,但是当我尝试提交我的字典,然后取回该信息时,它不起作用。 import pickle data = {'health':100, 'gold'
我读取了一个只包含一行的文件。但是,在循环结束之前我无法停止读取文件。即 python 不会抛出 EOFError 异常。我的代码有什么问题吗? for x in range(5): try:
我有一个简单的程序,我使用 pygame 导入声音,但它仍然是一个 CLI 程序。每当我将它编译成一个 exe 时,当我尝试运行它时它会给我一个 EOFError,它看起来像这样 Traceback
我最近发现了 pickle,这很神奇。但是当它用于我的实际脚本时,它对我来说是错误的,用一个单项字典测试它工作正常。我的真实脚本是数千行代码,其中存储了 Maya 中的各种对象。我不知道它是否与大小有
我有几个脚本在服务器上运行,可以对各种词典进行 pickle 和 unpickle。它们都使用相同的基本代码进行 pickle ,如下所示: SellerDict=open('/home/hostad
我有以下代码片段: def database(self): databasename="" host="" user="" password="" try:
我正在读取 CSV 作为 Spark DataFrame 并对其执行机器学习操作。我不断收到 Python 序列化 EOFError - 知道为什么吗?我认为这可能是一个内存问题——即文件超出了可用
当我运行下面的代码时,我收到此错误消息“EOFError: Ran out of input”这是什么意思??怎么才能矫正??以及如何在屏幕上输出记录的详细信息。 import pickle # th
为什么我在运行 django 服务器时会出现以下错误,我该如何解决? : EOFError: marshal data too short 细节 Performing system checks...
尝试创建用户时,我在生产中遇到此错误(我正在使用devise gem)。 EOFError (end of file reached): 我之前遇到此问题,这是由于我使用zoho mail的smtp设
我正在努力解决这一问题,我们将不胜感激! 我有两个Rails应用程序,我们称它们为Client and Service,它们都是非常简单的普通REST界面-这是基本方案: 客户端向服务发出POST/r
一切正常,直到: celery beat v3.1.18 (Cipater) is starting. __ - ... __ - _ Configuration ->
我是一名优秀的程序员,十分优秀!