简体   繁体   English

如何在不使用泡菜的情况下将对象保存在 Python 中?

[英]How to save objects in Python without using pickle?

I want to save a object in a file so i can use it later (after the program closes).我想将 object 保存在一个文件中,以便稍后(程序关闭后)使用它。

I tried to use pickle, but looks like it doesn't like my object:D我尝试使用泡菜,但看起来它不喜欢我的 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'

here is the code:这是代码:

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

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.我的MyWeirdClass() class 工作得非常好,我多次测试它,我得到了我预期的结果。

EDIT:编辑:

i found out that the problem is that one of the object's variables is a selenium.webdriver.chrome.webdriver.WebDriver object.我发现问题在于对象的变量之一是selenium.webdriver.chrome.webdriver.WebDriver object。 after deleting this object (after doing what i wanted with it) it worked fine.在删除这个 object 之后(在做了我想要的之后)它工作正常。

SECOND EDIT:第二次编辑:

i also got this error:我也收到了这个错误:

RecursionError: maximum recursion depth exceeded while pickling an object

On the line of code i tried to write the object to file.在代码行上,我尝试将 object 写入文件。

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:我发现我必须将sys.setrecursionlimit()设置为更高的值,所以在没有错误之前不要将其设置为随机值,而是这样做:

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

The easiest solution is going to be to define your class in such a way that it's pickle-able.最简单的解决方案是以可以腌制的方式定义 class 。 The error message suggests that some of your class's attributes can't be pickled because they don't have globally-scoped names.该错误消息表明您的某些类的属性不能被腌制,因为它们没有全局范围的名称。

If you want to save an object that's not pickleable, you'll need to write your own logic to serialize and deserialize it.如果要保存不可腌制的 object,则需要编写自己的逻辑来对其进行序列化和反序列化。 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.如果没有看到 object,就不可能就此给出具体建议,但总体思路是,您需要弄清楚如何将对象的 state 表示为可以腌制的东西(如一系列简单的字符串/整数属性),然后编写一个 function 将从该数据重构您的 object。

You can try HDF5, however, what kind of object do you want to save?你可以试试HDF5,但是你想保存什么样的object呢? https://pypi.org/project/h5py/ https://pypi.org/project/h5py/

It's annoying that you have to make it pickable, but there are other ways to save an object.令人讨厌的是您必须使其可挑选,但还有其他方法可以保存 object。

Try this:尝试这个:

>>> import h5py
>>> import numpy as np
>>> f = h5py.File("mytestfile.hdf5", "w") 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM