为什么这个简单的直方图字典代码说单词没有定义?

Why is this simple histogram dictionary code saying that words is not defined?

这是代码:

name = input("Enter file: ")
handle = open(name)

counts = dict()
filetext = handle.read()
for line in handle:
    words = line.split()
    for word in words:
        counts[word] = counts.get(word, 0) + 1
print(words)
print(counts)
bigcount = None
bigword = None
for word,count in counts.items():
    if bigcount == None or count > bigcount:
        bigword = word
        bigcount = count

print(filetext)
print("Most common word: ", bigword, bigcount)
print(counts.items())

this is the output:
Enter file: pls.txt
Traceback (most recent call last):
  File "D:\Tools\Coding\PyCharm Community Edition 
2021.2.3\bin\pythonProject2\Mostcommonword.py", line 10, in <module>
    print(words)
NameError: name 'words' is not defined

进程已完成,退出代码为 1

当运行程序时,它没有返回最常见的数字,而是返回了None。我设法找出原因是“单词”列表出于某种原因完全是空的。简单问题的好处是我知道发生了什么。糟糕的是根本没有多少方法可以修复它。

执行 handle.read() 后,文件位于 end-of-file。 for line in handle: 没有什么可读的了。您要么需要在两者之间倒回 (handle.seek(0)),要么完全跳过第一次阅读。

您可以考虑在遍历文件句柄时构建一个行列表,而不是执行 read/seek。此外,您可以在浏览文件时计算出最常出现的单词,而不用再翻一遍字典。像这样:

D = dict()
M = 0
B = None
C = list()
with open('<Your filename goes here>') as txt:
    for line in txt:
        C.append(line)
        for word in line.strip().split():
            D[word] = D.get(word, 0) + 1
            if D[word] > M:
                B = word
                M = D[word]

print(''.join(C))
print(f'Most common word with {M} occurrences is {B}')