テキストを取り込み、1 行ずつ読み取ることができる Python コードを作成しようとしています。各行では、単語はキーとして辞書に入力され、数字はリストとして割り当てられた値である必要があります。たとえば、ファイルは次のような形式の何百もの行で構成されます。
ピーター 17 29 24 284 72
理想的には、"Peter" という名前がディクショナリのキーであり、値がdict[Peter]: [17, 19, 24, 284,7273]
.
これまでのところ、私の問題は数字を追加することです。それらをキー値に割り当てる方法がわかりません。
def wordDict(filename):
inFile=open(filename, 'r')
line=inFile.readline()
while line:
txtWords = line.split() # splits at white space
wordScores={} # make dict
scoreList=[]
for word in txtWords:
word.lower() # turns word into lowercase
if word in string.ascii_lowercase: #if word is alphabetical
if word not in wordScores.keys():
wordScores=wordScores[word] # add the key to dictionary
---------- 私が持っているすべて