2013-09-23 327 views
6

我想學習如何在並保存一個對象在python中。但是,當我使用下面的sample code時,我得到以下錯誤:io.UnsupportedOperation: read其追溯到favorite_color = pickle.load(f_myfile)。我找不到這個特定錯誤的好解釋。我做錯了什麼,我該如何糾正?在Python中酸洗錯誤:io.UnsupportedOperation:讀

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

這就是複製和粘貼時發生的情況。 – cdarke

回答

16

變化:

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

到:

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

,你可以看到字典的obj你醃。

+0

令人難以置信的是,我不得不穀歌這個'錯誤'發現我是多麼愚蠢。謝謝! –