我正在尝试学习pickle
如何在python中保存对象。但是,当我使用下面的sample code时,我收到以下错误: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()
答案 0 :(得分:28)
变化:
# Read from file
f_myfile = open('myfile.pickle', 'wb')
到:
f_myfile = open('myfile.pickle', 'rb')
你可以看到你腌制的dict obj。