我使用以下代码在不打开文件的情况下腌制了python变量,但没有文件我似乎无法解压缩它。
import pickle
class Foo:
attr = 'a class attr'
picklestring = pickle.dumps(Foo)
print(picklestring)
答案 0 :(得分:2)
要从字节对象释放,请使用pickle.loads
:
In [3]: import pickle
...:
...: class Foo:
...: attr = 'a class attr'
...:
...: picklestring = pickle.dumps(Foo)
...:
...: print(picklestring)
...:
b'\x80\x03c__main__\nFoo\nq\x00.'
In [4]: pickle.loads(picklestring)
Out[4]: __main__.Foo