Python中的错误腌制:io.UnsupportedOperation:已读取 [英] Error Pickling in Python: io.UnsupportedOperation: read

查看:1045
本文介绍了Python中的错误腌制:io.UnsupportedOperation:已读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何pickle并在python中保存对象.但是,当我在下面使用示例代码时,出现以下错误:io.UnsupportedOperation: read追​​溯到favorite_color = pickle.load(f_myfile).对于此特定错误,我找不到很好的解释.我在做什么错了,我该如何纠正?

I am trying to learn how to pickle and save an object in python. However, when I use the sample code below I get the following error: io.UnsupportedOperation: read which traces back to favorite_color = pickle.load(f_myfile). I cannot find a good explanation of this particular error. What am I doing wrong and how do I correct it?

import pickle  # or import cPickle as pickle

# Create dictionary, list, etc.
favorite_color = { "lion": "yellow", "kitty": "red" }

# Write to file
f_myfile = open('myfile.pickle', 'wb')
pickle.dump(favorite_color, f_myfile)
f_myfile.close()

# Read from file
f_myfile = open('myfile.pickle', 'wb')
favorite_color = pickle.load(f_myfile)  # variables come out in the order you put them in
f_myfile.close()

推荐答案

更改:

# Read from file 
f_myfile = open('myfile.pickle', 'wb')

收件人:

f_myfile = open('myfile.pickle', 'rb')

,您可以看到您腌制的字典obj.

and you can see the dict obj you've pickled.

这篇关于Python中的错误腌制:io.UnsupportedOperation:已读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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