我制作了一个程序来检查文件中的单词是否需要一些建议

时间:2019-07-19 17:17:06

标签: python

我想打印单词是否出现以及单词在文件中出现的次数。除了这个单词在文件中出现1次或0次之外,我无法说什么。

This problem occurs on line 26, print("It appears " + str(wordcount[word]) + " times")

特别是str(wordcount [word])。这可能是一个简单的问题,但这是我学习python的第一周,所以如果有人有想法,请分享。谢谢!

我尝试放入wordcount[word]word_counter.__getitem__(wordcount[word])word_counter.__getitem__(wordcount)

import collections
file = open(r"C:\Users\Patrick Wu\Documents\1wordfreqtest.txt", "r")
if file.mode == "r":
    contents = file.read()
word = input("Word to check for :")
wordcount = {}
"""this below is to remove difference between upper and lower cases as 
well as punctuation""" 
for word in contents.lower().split():
    word = word.replace(".","")
    word = word.replace(",","")
    word = word.replace(":","")
    word = word.replace("\"","")
    word = word.replace("!","")
    word = word.replace("“","")
    word = word.replace("‘","")
    word = word.replace("*","")
    if word not in wordcount:
        wordcount[word] = 1
    else:
        wordcount[word] += 1

word_counter = collections.Counter(wordcount)
if word in str(contents):
    print("This word is in the file :)")
    print("It appears " + str(wordcount[word]) + " times")
else:
    print("This word isn't in the file")

3 个答案:

答案 0 :(得分:1)

变量word在本地作用域中被循环覆盖。因此,您的输入word被循环覆盖,最终检查输入文件的最后一个单词的计数。将输入的单词更改为与文件中迭代的单词不同的变量名。

答案 1 :(得分:1)

您可以使用以下代码:

import collections
file = open("wordfreqtest.txt", "r")
if file.mode == "r":
    contents = file.read().lower()
word = input("Word to check for :").lower()

times = 0

finish = 0

while finish==0:
    if word in contents:
        contents = contents[contents.find(word) + len(word):]
        times += 1
    else:
        break

if times > 0:
    print("This word is in the file :)")
    print("It appears " + str(times) + " times")
else:
    print("This word isn't in the file")

答案 2 :(得分:1)

通过在输入和for循环中使用相同的名称“单词”,您会遇到范围问题。

我建议做这样的事情:

word = input("Word to check for :")
with open('your_file.txt') as f:
     raw = f.read()
     num_appearences = raw.count(word)
     print(f"The word {word} appears {num_appearences} times in the file")