我正在使用架子课来做我的工作。但是我不知道为什么它不起作用。
这就是我在做什么:

import shelve

file = shelve.open('data.db')
shelf = shelve.Shelf(file)

shelf['first'] = [1, 2, 3]
file.close()
shelf.close()


给我一个错误,说“字节”对象没有属性“编码”
顺便说一句我正在使用python 3.5

最佳答案

您不需要以下行:

shelf = shelve.Shelf(file)


这是一个工作示例:

import shelve

shelf = shelve.open('data.db')

shelf['first'] = [1, 2, 3]
shelf.close()


或更有效的方法:

import shelve

with shelve.open('data.db') as shelf:
    shelf['first'] = [1, 2, 3]

10-06 10:57