私はファイル内に孤立した辞書 '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
コードがループバックすると、コードは最初の実行時に動作します。 (私はこれがの一部ではないと思います
shelf = shelve.open("word_list.dat")
shelf[(new_txt_file)] = new_text_list
shelf['word_dictionary'] = (shelf['word_dictionary']).update({(new_dictionary_name):(new_dictionary_name)})
#not updating
shelf.sync()
shelf.close()
そして、これは更新が完了しません後に動作しないコードです:
これは、辞書を更新するために意図されたコードです問題はありますが間違っている可能性があります)
shelf = shelve.open("word_list.dat")
shelf.sync()
word_dictionary= dict(shelf['word_dictionary'])
ご協力いただきありがとうございます。 UPDATE これはインポートされ、私は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']'の中に何を入れているのですか?あなたは、2番目のスニペットでそれ自体の価値として棚を設定しているようです。 – amirouche
どちらのスニペットも、shelvedリストのあるファイルとは異なる.pyファイルから取得し、そのファイルにインポートします。それは辞書を漬けているファイルで である: word_dictionary = {「ホープ単語」:「hope_words」、「メリー単語」:「merry_words」、「驚嘆単語」:「amazement_words」} 私がword_dictionary設定私はコードの残りの部分で毎回シェルフを参照する必要はありません。棚を使っているのは初めてのことなので、これが必要でないなら私に修正してください! – Jess