PHP
·
发表于 5年以前
·
阅读量:8296
使用pickle的dumps()方法可以将对象转换成字符串形式,随后用字节文本形式存储对象到文件
import pickle
class Bird(object):
have_feather = True
summer = Bird()
pickle_string = pickle.dumps(summer)
with open("summer.pkl", "wb") as f:
f.write(pickle_string)
也可以使用dump方法一步到位
import pickle
class Bird(object):
have_feather = True
summer = Bird()
with open("summer.pkl", "w") as f:
pickle.dump(summer, f)