Python 更新写入文件的搁置词典

Python 更新写入文件的搁置词典,python,dictionary,updating,shelve,Python,Dictionary,Updating,Shelve,我在文件中有一个搁置的字典“word_dictionary”,我可以在主程序中访问它。我需要让用户能够向字典中添加条目。但我无法将条目保存在搁置的词典中,因此出现错误: Traceback (most recent call last): File "/Users/Jess/Documents/Python/Coursework/Coursework.py", line 16, in <module> word_dictionary= dict(shelf['word_d

我在文件中有一个搁置的字典“word_dictionary”,我可以在主程序中访问它。我需要让用户能够向字典中添加条目。但我无法将条目保存在搁置的词典中,因此出现错误:

Traceback (most recent call last):
  File "/Users/Jess/Documents/Python/Coursework/Coursework.py", line 16, in <module>
    word_dictionary= dict(shelf['word_dictionary'])
TypeError: 'NoneType' object is not iterable
这是更新未完成后无法工作的代码(我不认为这是问题的一部分,但我可能错了)

提前感谢您的帮助和耐心! 更新 这是我调用导入的word_dictionary的代码的开始:

while True:
 shelf = shelve.open("word_list.dat")
 print('{}'.format(shelf['word_dictionary']))
 word_dictionary= dict(shelf['word_dictionary'])
 print(word_dictionary)
 word_keys = list(word_dictionary.keys())
 shelf.close()
这就是我要添加到的原始词典的位置:

shelf['word_dictionary'] = {'Hope Words': 'hope_words', 'Merry Words': 'merry_words', 'Amazement Words': 'amazement_words'}

问题是,必须将搁置数据库更新与数据库加载到内存中的对象分开

shelf['word_dictionary'] = (shelf['word_dictionary']).update({(new_dictionary_name):(new_dictionary_name)})
此代码将
dict
加载到内存中,称为其
update
方法,将
update
方法的结果分配回工具架,然后删除已更新的内存字典。但是
dict.update
不返回任何值,并且您完全重写了词典。将dict放入变量中,更新,然后保存变量

words = shelf['word_dictionary']
words.update({(new_dictionary_name):(new_dictionary_name)})
shelf['word_dictionary'] = words
更新

当工具架关闭时,是否保存新数据存在问题。这里有一个例子

# Create a shelf with foo
>>> import shelve
>>> shelf = shelve.open('word_list.dat')
>>> shelf['foo'] = {'bar':1}
>>> shelf.close()

# Open the shelf and its still there
>>> shelf = shelve.open('word_list.dat')
>>> shelf['foo']
{'bar': 1}

# Add baz
>>> data = shelf['foo']
>>> data['baz'] = 2
>>> shelf['foo'] = data
>>> shelf.close()

# Its still there
>>> shelf = shelve.open('word_list.dat')
>>> shelf['foo']
{'baz': 2, 'bar': 1}

问题是,必须将搁置数据库更新与数据库加载到内存中的对象分开

shelf['word_dictionary'] = (shelf['word_dictionary']).update({(new_dictionary_name):(new_dictionary_name)})
此代码将
dict
加载到内存中,称为其
update
方法,将
update
方法的结果分配回工具架,然后删除已更新的内存字典。但是
dict.update
不返回任何值,并且您完全重写了词典。将dict放入变量中,更新,然后保存变量

words = shelf['word_dictionary']
words.update({(new_dictionary_name):(new_dictionary_name)})
shelf['word_dictionary'] = words
更新

当工具架关闭时,是否保存新数据存在问题。这里有一个例子

# Create a shelf with foo
>>> import shelve
>>> shelf = shelve.open('word_list.dat')
>>> shelf['foo'] = {'bar':1}
>>> shelf.close()

# Open the shelf and its still there
>>> shelf = shelve.open('word_list.dat')
>>> shelf['foo']
{'bar': 1}

# Add baz
>>> data = shelf['foo']
>>> data['baz'] = 2
>>> shelf['foo'] = data
>>> shelf.close()

# Its still there
>>> shelf = shelve.open('word_list.dat')
>>> shelf['foo']
{'baz': 2, 'bar': 1}

我不知道你把什么东西放进了
书架['word\u dictionary']
?在第二个代码段中,您似乎将shelf设置为其自身的值。这两个代码段都来自一个不同的.py文件,而不是具有搁置列表的文件,然后我将其导入该文件。在被腌制的文件中,字典是:word_dictionary={'Hope Words':'Hope_Words','Merry Words':'Merry_Words','amazed Words':'amazed_Words'}我将word_dictionary自身设置为我不必在代码的其余部分每次都参考书架。这是我第一次使用书架,如果没有必要,请纠正我!我不知道你把什么东西放进了
书架['word\u dictionary']
?在第二个代码段中,您似乎将shelf设置为其自身的值。这两个代码段都来自一个不同的.py文件,而不是具有搁置列表的文件,然后我将其导入该文件。在被腌制的文件中,字典是:word_dictionary={'Hope Words':'Hope_Words','Merry Words':'Merry_Words','amazed Words':'amazed_Words'}我将word_dictionary自身设置为我不必在代码的其余部分每次都参考书架。这是我第一次使用书架,如果没有必要,请纠正我!非常感谢你,你减轻了几个小时的头痛!我几乎绝望地走向瓶子哈哈。这种方法真的很好——但如果我关闭文件并再次打开它,它似乎不会永久保存它?@Jess我添加了一个更新存活的例子
close
。如果您有问题,这可能是代码中的错误。该列表保留在shelf.close()中,但如果在用户输入新值后关闭模块,则在重新打开模块并运行该模块时,用户所做的添加将被删除gone@Jess“关闭模块”是指退出程序并再次执行。它应该能很好地保存下来。也许在代码的早期有一个地方可以覆盖它或删除文件或其他东西。你能发布一个简短的可运行的例子来演示这个问题吗?这是最好的解决办法。非常感谢你——你已经缓解了几个小时的头痛!我几乎绝望地走向瓶子哈哈。这种方法真的很好——但如果我关闭文件并再次打开它,它似乎不会永久保存它?@Jess我添加了一个更新存活的例子
close
。如果您有问题,这可能是代码中的错误。该列表保留在shelf.close()中,但如果在用户输入新值后关闭模块,则在重新打开模块并运行该模块时,用户所做的添加将被删除gone@Jess“关闭模块”是指退出程序并再次执行。它应该能很好地保存下来。也许在代码的早期有一个地方可以覆盖它或删除文件或其他东西。你能发布一个简短的可运行的例子来演示这个问题吗?这是解决问题的最好办法。