我正在尝试学习如何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()