如何在不使用pickle的情况下在Python中保存对象? [英] How to save objects in Python without using pickle?

查看:104
本文介绍了如何在不使用pickle的情况下在Python中保存对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个对象保存在一个文件中,以便稍后(在程序关闭后)使用它.

I want to save a object in a file so i can use it later (after the program closes).

我尝试使用泡菜,但看起来它不喜欢我的对象 :D

I tried to use pickle, but looks like it doesn't like my object :D

Traceback (most recent call last):
  File "(path)", line 4, in <module>
    pickle.dump(object, f)
AttributeError: Can't pickle local object '_createenviron.<locals>.encodekey'

代码如下:

import pickle
object = MyWeirdClass()
with open("data.pickle", "wb") as f:
    pickle.dump(object, f)

还有其他保存对象的方法吗(比如外部库)?我做错了什么,我得到了这个错误?我的 MyWeirdClass() 类运行良好,我对其进行了多次测试,结果完全符合预期.

There is any other way to save objects (like an external library)? I did something wrong and i got this error? My MyWeirdClass() class is working perfectly fine, i tested it multiple times and i got exactly the results i expected.

我发现问题在于对象的变量之一是 selenium.webdriver.chrome.webdriver.WebDriver目的.删除这个对象后(在用它做我想做的事情之后)它工作正常.

i found out that the problem is that one of the object's variables is a selenium.webdriver.chrome.webdriver.WebDriver object. after deleting this object (after doing what i wanted with it) it worked fine.

第二次

我也遇到了这个错误:

RecursionError: maximum recursion depth exceeded while pickling an object

在代码行上,我尝试将对象写入文件.

On the line of code i tried to write the object to file.

我发现我必须将 sys.setrecursionlimit() 设置为更高的值,因此我没有将其设置为随机值直到没有错误,而是这样做:

I found out that i have to set the sys.setrecursionlimit() to a higher value, so instead of setting it to random values until i got no errors, i make this:

import pickle
import sys
default_cursion_limit = sys.getrecursionlimit()# defalut is 1000
object = MyWeirdClass()
while True:
    try:
        with open("data.pickle", "wb") as f:
            pickle.dump(object, f)
        break
    except RecursionError:
        default_cursion_limit += 50
        sys.setrecursionlimit(default_cursion_limit)# looks like its working with 2600

推荐答案

最简单的解决方案是以一种可以pickle 的方式定义您的类.该错误消息表明您的类的某些属性无法进行腌制,因为它们没有全局范围的名称.

The easiest solution is going to be to define your class in such a way that it's pickle-able. The error message suggests that some of your class's attributes can't be pickled because they don't have globally-scoped names.

如果要保存不可pickle的对象,则需要编写自己的逻辑来对其进行序列化和反序列化.在没有看到对象的情况下不可能就此给出具体的建议,但一般的想法是,您需要弄清楚如何将对象的状态表示为可以腌制的东西(如一系列简单的字符串/int 属性),然后编写一个将根据该数据重构您的对象的函数.

If you want to save an object that's not pickleable, you'll need to write your own logic to serialize and deserialize it. It's impossible to give specific advice on this without seeing the object, but the general idea is that you need to figure out how to represent your object's state as something that you CAN pickle (like a series of simple string/int attributes) and then write a function that will reconstitute your object from that data.

这篇关于如何在不使用pickle的情况下在Python中保存对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆