ファイルを辞書に読み込もうとしているので、キーが単語で、値が単語の出現数になります。私は動作するはず何かを持っているが、私はそれを実行すると、これは私が今持っているものである私にPythonを使用して辞書にファイルを読み込むときにバリューエラーが発生する
ValueError: I/O operation on closed file.
を与える:
try:
f = open('fileText.txt', 'r+')
except:
f = open('fileText.txt', 'a')
def read_dictionary(fileName):
dict_word = {} #### creates empty dictionary
file = f.read()
file = file.replace('\n', ' ').rstrip()
words = file.split(' ')
f.close()
for x in words:
if x not in result:
dict_word[x] = 1
else:
dict_word[x] += 1
print(dict_word)
print read_dictionary(f)
あなたの変数は実際にはファイルハンドルです。 'file'という変数はファイルのテキスト内容です。少なくとも、あなたの名前が自分に割り当てられたものを記述した場合、問題がどこにあるのかを簡単に判断することができます。 – jonrsharpe