Python 中的错误酸洗:io.UnsupportedOperation: read
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18963949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error Pickling in Python: io.UnsupportedOperation: read
提问by Michael
I am trying to learn how to pickle
and save an object in python. However, when I use the sample codebelow 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?
我正在尝试学习如何pickle
在 python 中保存一个对象。但是,当我使用下面的示例代码时,出现以下错误:io.UnsupportedOperation: read
可追溯到favorite_color = pickle.load(f_myfile)
. 我找不到这个特定错误的一个很好的解释。我做错了什么,我该如何纠正?
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()
采纳答案by Jay Choo
Change:
改变:
# Read from file
f_myfile = open('myfile.pickle', 'wb')
to:
到:
f_myfile = open('myfile.pickle', 'rb')
and you can see the dict obj you've pickled.
你可以看到你腌制的 dict obj 。