[英]How to save instance object to hard drive
我创建了一个这样的实例:
class some_class():
def __init__(self, aa, bb):
self.a = aa
self.b = bb
def add(self):
self.c = self.a + self.b
return self.c
instance001 = some_class(2,200)
现在,我尝试将instance001
保存到硬盘上,以备将来使用。
with open('Storage', 'w') as file:
file.write(instance001)
这行不通。 如何存储实例?
首选格式是hdf,但是其他任何想法都可以接受。
注意:大量使用熊猫。
如果是纯Python类,则可以简单地使用pickle
:
import pickle
with open('Storage', 'wb') as f:
pickle.dump(instance001, f)
并加载它:
with open('Storage', 'rb') as f:
instance002 = pickle.load(f)
print(instance002.a) # 2
print(instance002.b) # 200
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.