保存对象(数据持久性)

保存对象(数据持久性)

我创建了这样一个对象:

company1.name = 'banana' company1.value = 40

我想保存这个对象。我怎么能这么做?


蓝山帝景
浏览 634回答 3
3回答

米琪卡哇伊

我认为假设对象是class..如果不是class?还有一个假设,即在解释器中没有定义对象。如果它是在解释器中定义的呢?另外,如果属性是动态添加的呢?当某些python对象将属性添加到它们的__dict__在创造之后,pickle不尊重这些属性的添加(也就是说,它“忘记”它们是添加的-因为pickle通过引用对象定义进行序列化)。在所有这些情况下,pickle和cPickle会让你失望的。如果您希望保存object(任意创建),其中有属性(或者添加在对象定义中,或者随后添加)…你最好的选择是dill,它可以序列化python中的几乎任何东西。我们从…类开始Python&nbsp;2.7.8&nbsp;(default,&nbsp;Jul&nbsp;13&nbsp;2014,&nbsp;02:29:54)&nbsp;[GCC&nbsp;4.2.1&nbsp;Compatible&nbsp;Apple&nbsp;Clang&nbsp;4.1&nbsp;((tags/Apple/clang-421.11.66))]&nbsp;on&nbsp;darwinType &nbsp;"help",&nbsp;"copyright",&nbsp;"credits"&nbsp;or&nbsp;"license"&nbsp;for&nbsp;more&nbsp;information.>>>&nbsp;import&nbsp;pickle>>>&nbsp;class&nbsp;Company:...&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;pass...&nbsp;>>>&nbsp;company1&nbsp;=&nbsp;Company()>>>&nbsp;company1.name&nbsp;=&nbsp;'banana'>>>&nbsp;company1.value&nbsp;=&nbsp;40>>>&nbsp;with&nbsp;open('company.pkl',&nbsp;'wb')&nbsp;as&nbsp;f:...&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pickle.dump(company1,&nbsp;f,&nbsp;pickle.HIGHEST_PROTOCOL)...&nbsp;>>>现在关闭,重新启动.。Python&nbsp;2.7.8&nbsp;(default,&nbsp;Jul&nbsp;13&nbsp;2014,&nbsp;02:29:54)&nbsp;[GCC&nbsp;4.2.1&nbsp;Compatible&nbsp;Apple&nbsp;Clang&nbsp;4.1&nbsp;((tags/Apple/clang-421.11.66))]&nbsp;on&nbsp;darwinType&nbsp;"help", &nbsp;"copyright",&nbsp;"credits"&nbsp;or&nbsp;"license"&nbsp;for&nbsp;more&nbsp;information.>>>&nbsp;import&nbsp;pickle>>>&nbsp;with&nbsp;open('company.pkl',&nbsp;'rb')&nbsp;as&nbsp;f:...&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;company1&nbsp;=&nbsp;pickle.load(f)...&nbsp;Traceback&nbsp;(most&nbsp;recent&nbsp;call&nbsp;last): &nbsp;&nbsp;File&nbsp;"<stdin>",&nbsp;line&nbsp;2,&nbsp;in&nbsp;<module> &nbsp;&nbsp;File&nbsp;"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py",&nbsp;line&nbsp;1378,&nbsp;in&nbsp;load&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;return&nbsp;Unpickler(file).load() &nbsp;&nbsp;File&nbsp;"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py",&nbsp;line&nbsp;858,&nbsp;in&nbsp;load dispatch[key](self) &nbsp;&nbsp;File&nbsp;"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py",&nbsp;line&nbsp;1090,&nbsp;in&nbsp;load_global &nbsp;&nbsp;&nbsp;&nbsp;klass&nbsp;=&nbsp;self.find_class(module,&nbsp;name) &nbsp;&nbsp;File&nbsp;"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py",&nbsp;line&nbsp;1126,&nbsp;in&nbsp;find_class &nbsp;&nbsp;&nbsp;&nbsp;klass&nbsp;=&nbsp;getattr(mod,&nbsp;name)AttributeError:&nbsp;'module'&nbsp;object&nbsp;has&nbsp;no&nbsp;attribute&nbsp;'Company'>>>OOPS…pickle处理不了。我们试试看dill..我们将添加另一个对象类型(lambda)为了好的措施。Python&nbsp;2.7.8&nbsp;(default,&nbsp;Jul&nbsp;13&nbsp;2014,&nbsp;02:29:54)&nbsp;[GCC&nbsp;4.2.1&nbsp;Compatible&nbsp;Apple&nbsp;Clang&nbsp;4.1&nbsp;((tags/Apple/clang-421.11.66))]&nbsp;on&nbsp;darwinType&nbsp;"help", &nbsp;"copyright",&nbsp;"credits"&nbsp;or&nbsp;"license"&nbsp;for&nbsp;more&nbsp;information.>>>&nbsp;import&nbsp;dill&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; >>>&nbsp;class&nbsp;Company:...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pass...&nbsp;>>>&nbsp;company1&nbsp;=&nbsp;Company()>>>&nbsp;company1.name&nbsp;=&nbsp;'banana'>>>&nbsp;company1.value&nbsp;=&nbsp;40>>>&nbsp;>>>&nbsp;company2&nbsp;=&nbsp; lambda&nbsp;x:x>>>&nbsp;company2.name&nbsp;=&nbsp;'rhubarb'>>>&nbsp;company2.value&nbsp;=&nbsp;42>>>&nbsp;>>>&nbsp;with&nbsp;open('company_dill.pkl',&nbsp;'wb')&nbsp;as&nbsp;f:...&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;dill.dump(company1,&nbsp;f)...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dill.dump(company2,&nbsp;f)...&nbsp;>>>现在看文件。Python&nbsp;2.7.8&nbsp;(default,&nbsp;Jul&nbsp;13&nbsp;2014,&nbsp;02:29:54)&nbsp;[GCC&nbsp;4.2.1&nbsp;Compatible&nbsp;Apple&nbsp;Clang&nbsp;4.1&nbsp;((tags/Apple/clang-421.11.66))]&nbsp;on&nbsp;darwinType&nbsp;"help", &nbsp;"copyright",&nbsp;"credits"&nbsp;or&nbsp;"license"&nbsp;for&nbsp;more&nbsp;information.>>>&nbsp;import&nbsp;dill>>>&nbsp;with&nbsp;open('company_dill.pkl',&nbsp;'rb')&nbsp;as&nbsp;f:...&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;company1&nbsp;=&nbsp;dill.load(f)...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;company2&nbsp;=&nbsp;dill.load(f)...&nbsp;>>>&nbsp;company1&nbsp; <__main__.Company&nbsp;instance&nbsp;at&nbsp;0x107909128>>>>&nbsp;company1.name'banana'>>>&nbsp;company1.value40>>>&nbsp;company2.name'rhubarb'>>>&nbsp;company2.value42>>>它起作用了。原因pickle失败,而且dill不是,是吗?dill款待__main__类似于一个模块(在大多数情况下),并且还可以通过引用对类定义进行分类,而不是对类进行分类(如pickle)。原因dill腌制lambda是它给它起了一个名字…然后腌制魔法就会发生。实际上,有一种更简单的方法来保存所有这些对象,特别是如果您已经创建了很多对象。只需转储整个python会话,稍后再返回。Python&nbsp;2.7.8&nbsp;(default,&nbsp;Jul&nbsp;13&nbsp;2014,&nbsp;02:29:54)&nbsp;[GCC&nbsp;4.2.1&nbsp;Compatible&nbsp;Apple&nbsp;Clang&nbsp;4.1&nbsp;((tags/Apple/clang-421.11.66))]&nbsp;on&nbsp;darwinType&nbsp;"help",&nbsp; "copyright",&nbsp;"credits"&nbsp;or&nbsp;"license"&nbsp;for&nbsp;more&nbsp;information.>>>&nbsp;import&nbsp;dill>>>&nbsp;class&nbsp;Company:...&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;pass...&nbsp;>>>&nbsp;company1&nbsp;=&nbsp;Company()>>>&nbsp;company1.name&nbsp;=&nbsp;'banana'>>>&nbsp;company1.value&nbsp;=&nbsp;40>>>&nbsp;>>>&nbsp;company2&nbsp;=&nbsp;lambda&nbsp;x:x>>>&nbsp;company2.name&nbsp;=&nbsp; &nbsp;'rhubarb'>>>&nbsp;company2.value&nbsp;=&nbsp;42>>>&nbsp;>>>&nbsp;dill.dump_session('dill.pkl')>>>关掉你的电脑,喝杯浓咖啡什么的,等会儿再来.Python&nbsp;2.7.8&nbsp;(default,&nbsp;Jul&nbsp;13&nbsp;2014,&nbsp;02:29:54)&nbsp;[GCC&nbsp;4.2.1&nbsp;Compatible&nbsp;Apple&nbsp;Clang&nbsp;4.1&nbsp;((tags/Apple/clang-421.11.66))]&nbsp;on&nbsp;darwinType&nbsp;"help",&nbsp; "copyright",&nbsp;"credits"&nbsp;or&nbsp;"license"&nbsp;for&nbsp;more&nbsp;information.>>>&nbsp;import&nbsp;dill>>>&nbsp;dill.load_session('dill.pkl')>>>&nbsp;company1.name'banana'>>>&nbsp; company1.value40>>>&nbsp;company2.name'rhubarb'>>>&nbsp;company2.value42>>>&nbsp;company2<function&nbsp;<lambda>&nbsp;at&nbsp;0x1065f2938>唯一的主要缺点是dill不是python标准库的一部分。因此,如果您无法在服务器上安装python包,那么就不能使用它。但是,如果您能够在系统上安装python包,则可以获得最新的dill带着git+https://github.com/uqfoundation/dill.git@master#egg=dill..您可以通过以下方式获得最新发布的版本pip install dill.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python