我正在使用 Python 3。shelve
在文档中宣传为dict
. 但是,在我的使用中,事实证明shelve
不允许元组作为键,而 dict 允许:
import shelve
def tryShelve():
db = shelve.open("shelvefile")
db["word1"] = 1
db[("word1", "word2")] = 15
tryShelve()
产生此错误:
Traceback (most recent call last):
File "<pyshell#41>", line 1, in <module>
tryShelve()
File "<pyshell#40>", line 4, in tryShelve
db[("word1", "word2")] = 15
File "C:\Python32\lib\shelve.py", line 125, in __setitem__
self.dict[key.encode(self.keyencoding)] = f.getvalue()
AttributeError: 'tuple' object has no attribute 'encode'