Error Pickling in Python: io.UnsupportedOperation: read

核能气质少年 提交于 2020-04-08 08:55:50

问题


I am trying to learn how to pickle and save an object in python. However, when I use the sample code below 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?

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()

回答1:


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.



来源:https://stackoverflow.com/questions/18963949/error-pickling-in-python-io-unsupportedoperation-read

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!