尝试检索搁置的类时出现KeyError和AtrributeError-Python 3.6

时间:2018-11-28 22:47:41

标签: python python-3.x shelve

在一个程序(带有搁置)中序列化类时,如果没有跟随错误,我将无法检索它:

 File "\Python36_64\lib\shelve.py", line 111, in __getitem__
    value = self.cache[key]
KeyError: 'foo'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "program.py", line 12, in <module>
    bar = db['foo']
  File "\Python36_64\lib\shelve.py", line 114, in __getitem__
    value = Unpickler(f).load()
AttributeError: Can't get attribute 'bar' on <module '__main__' (built-in)>

这是我初始化架子的代码。它编译:

import shelve 
class bar:
   x = {}
db = shelve.open('file.dat')
db['foo'] = bar

我一直在尝试使用以下代码在另一个程序中检索类栏。这没有正确编译。:

import shelve
db = shelve.open('file.dat')
bar = db['foo']

1 个答案:

答案 0 :(得分:1)

您不能通过这样的方式腌制和取消腌制来存储类。当with VW_GROUPED as ( select t.*, (select count(*) from TheTable x where x.URN <= t.URN and x.GAPNOOFDAYS > 1) as GROUPNO from TheTable /* <- your table name here */ t) select g.URN, g.CUSTOMER_ID, g.GAPNOOFDAYS, row_number() over (partition by GROUPNO order by URN) as "Counter I need to generate" from VW_GROUPED g 需要腌制一个类时,它仅记录该类的模块和名称,而不是内容。只能在相同模块定义了相同类的环境中才能取消腌制。