pickle.load() 在 Windows 中引发 EOFError [英] pickle.load() raising EOFError in Windows

查看:21
本文介绍了pickle.load() 在 Windows 中引发 EOFError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码是这样的

with open(pickle_f, 'r') as fhand:
    obj = pickle.load(fhand)

这在 Linux 系统上运行良好,但不适用于 Windows.它显示EOFError.我必须使用 rb 模式才能使其在 Windows 上运行..现在这不适用于 Linux.

This works fine on Linux systems but not on Windows. Its showing EOFError. I have to use rb mode to make it work on Windows.. now this isn't working on Linux.

为什么会发生这种情况,以及如何解决?

Why this is happening, and how to fix it?

推荐答案

读写pickles时总是使用b模式(open(f, 'wb') for写作,open(f, 'rb') 用于阅读).要修复"您已有的文件,请使用 dos2unix 转换其换行符.

Always use b mode when reading and writing pickles (open(f, 'wb') for writing, open(f, 'rb') for reading). To "fix" the file you already have, convert its newlines using dos2unix.

这篇关于pickle.load() 在 Windows 中引发 EOFError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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