Python:使用 pickle 模块保存和加载对象时出错

发布于 2024-10-10 02:33:52 字数 1150 浏览 4 评论 0原文

我正在尝试使用这段代码加载和保存对象,这是我从一周前问的一个问题中得到的: Python:保存和加载对象并使用 pickle。

这段代码是这样的:

class Fruits: pass

banana = Fruits()    
banana.color = 'yellow'     
banana.value = 30

import pickle

filehandler = open("Fruits.obj","wb")    
pickle.dump(banana,filehandler)    
filehandler.close()

file = open("Fruits.obj",'rb')    
object_file = pickle.load(file)    
file.close()

print(object_file.color, object_file.value, sep=', ')

乍一看,这段代码运行良好,加载并看到“颜色”和已保存对象的“值”。 但是,我追求的是关闭一个会话,打开一个新会话并加载我在过去的会话中保存的内容。我在放置 filehandler.close() 行后关闭会话,然后打开一个新会话,然后放置其余代码,然后在放置 object_file = pickle.load(file)< /code> 我收到此错误:

Traceback (most recent call last): 
File "<pyshell#5>", line 1, in <module> 
object_file = pickle.load(file) 
File "C:\Python31\lib\pickle.py", line 1365, in load 
encoding=encoding, errors=errors).load() 
AttributeError: 'module' object has no attribute 'Fruits' 

任何人都可以解释一下此错误消息的含义并告诉我如何解决此问题吗?

非常感谢,新年快乐!!

I am trying to load and save objects with this piece of code I get it from a question I asked a week ago: Python: saving and loading objects and using pickle.

The piece of code is this:

class Fruits: pass

banana = Fruits()    
banana.color = 'yellow'     
banana.value = 30

import pickle

filehandler = open("Fruits.obj","wb")    
pickle.dump(banana,filehandler)    
filehandler.close()

file = open("Fruits.obj",'rb')    
object_file = pickle.load(file)    
file.close()

print(object_file.color, object_file.value, sep=', ')

At a first glance the piece of code works well, getting load and see the 'color' and 'value' of the saved object.
But, what I pursuit is to close a session, open a new one and load what I save in a past session. I close the session after putting the line filehandler.close() and I open a new one and I put the rest of your code, then after putting object_file = pickle.load(file) I get this error:

Traceback (most recent call last): 
File "<pyshell#5>", line 1, in <module> 
object_file = pickle.load(file) 
File "C:\Python31\lib\pickle.py", line 1365, in load 
encoding=encoding, errors=errors).load() 
AttributeError: 'module' object has no attribute 'Fruits' 

Can anyone explain me what this error message means and telling me how to solve this problem?

Thank so much and happy new year!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉殇 2024-10-17 02:33:52

Python 不会 pickle 整个类。只有名字。因此,您必须将包含它们的模块保存到文件中,并在解封它们时可导入。然后 Python 将重新导入它们。

如果遇到问题,您可能需要定义用于酸洗的特殊辅助方法,__getstate____setstate__

Python does not pickle whole classes. Only the names. Therefore you must have the module that contains them saved to a file and importable at the time they are unpickled. Python will then re-import them.

If you run into problems, you may need to define special helper methods, __getstate__ and __setstate__ that are used for pickling.

不一样的天空 2024-10-17 02:33:52

所以...这是许多练习 OOP 的从业者不使用 pickle 的原因之一——它对于 serde 来说太可怕了。特别是当您移动名称空间时,对象定义可能不存在!那么,有什么替代方案呢?

有两种选择:莳萝和云泡菜。我更喜欢莳萝而不是云泡菜 - 但两者都适合你的情况。使用 pip 安装,然后执行诸如

import dill as pickle

import cloudpickle as pickle

之类的操作,然后享受您的午餐。

So... This is one of the reason pickle is not used by many practitioners who practice OOP - it is horrible for serde. Especially when you shift namespace and the object definition may not be present! So, what is an alternative?

There are two alternatives, dill and cloudpickle. I prefer dill over cloudpickle - but both will work for your case. Install either with pip, then do somehting like

import dill as pickle

or

import cloudpickle as pickle

and then enjoy your lunck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文