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()
這就是複製和粘貼時發生的情況。 – cdarke