将文件中的单词读入字典 [英] Read words from file into dictionary
问题描述
因此,在我们的作业中,我的教授希望我们逐行阅读文本文件,然后逐字阅读,然后创建一个字典,计算出每个单词出现的频率.这是我现在拥有的:
so in our assignment my professor would like us to read in a text file line by line, then word by word, then create a dictionary counting the frequency of each word appearing. Here's what I have for now:
wordcount = {}
with open('/Users/user/Desktop/Text.txt', 'r', encoding='utf-8') as f:
for line in f:
for word in line.split():
line = line.lower()
word = word.strip(string.punctuation + string.digits)
if word:
wordcount[word] = line.count(word)
return wordcount
发生的是,我的词典告诉我每个单词在特定行中出现了多少,而当某些单词多次出现在整个文本中时,我剩下的多数为1.我如何才能使字典来计算整个文本中的单词,而不仅仅是一行?
What happens is that my dictionary tells me how many of each word appears in a particular line, leaving me with mostly 1s when some words show up in the entire text many times. How can I get my dictionary to count words from the entire text, not just a line?
推荐答案
问题是您每次都要重置它,解决方法很简单:
The problem is you are resetting it every time, the fix is quite simple:
wordcount = {}
with open('/Users/user/Desktop/Text.txt', 'r', encoding='utf-8') as f:
for line in f:
for word in line.split():
line = line.lower()
word = word.strip(string.punctuation + string.digits)
if word:
if word in wordcount:
wordcount[word] += line.count(word)
else:
wordcount[word] = line.count(word)
return wordcount
这篇关于将文件中的单词读入字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!