计算文本文件中的所需单词

时间:2018-11-15 01:07:00

标签: python python-3.x file loops counting

我必须计算给定单词在给定文本文件中出现的次数,这就是葛底斯堡地址。由于某种原因,它没有计算我的“国家”输入,因此输出看起来像这样:

'nation' is found 0 times in the file gettysburg.txt

这是我当前拥有的代码,有人可以指出我在做什么吗?

fname = input("Enter a file name to process:")
find = input("Enter a word to search for:")
text = open(fname, 'r').read()
def processone():
    if text is not None:
        words = text.lower().split()
        return words
    else:
        return None

def count_word(tokens, token):
    count = 0
    for element in tokens:
        word = element.replace(",", " ")
        word = word.replace("."," ")

        if word == token:
            count += 1
        return count
words = processone()
word = find
frequency = count_word(words, word)
print("'"+find+"'", "is found", str(frequency), "times in the file", fname)

我的第一个函数将文件拆分为字符串,并将所有字母都转换为小写字母。第二个删除标点符号,并应该计算输入中给定的单词。

参加我的第一个编码班,如果您发现我的编码中有更多缺陷或可以进行的改进,以及帮助找到解决问题的方法,请放心。

3 个答案:

答案 0 :(得分:4)

for函数的count_word()循环中,循环的末尾有一个return语句,该语句仅在一个循环迭代后立即退出该函数。

您可能希望将return语句移到for循环之外。

答案 1 :(得分:0)

首先,我建议您使用打印语句并查看正在打印的变量,这有助于解决问题。例如,打印 word 仅显示文件中的第一个单词,这可能会解释您的代码中的问题。

def count_word(tokens, token):
    count = 0
    for element in tokens:
        word = element.replace(",", " ")
        word = word.replace("."," ")
        print (word)
        if word == token:
            count += 1
        return count


Enter a file name to process:gettysburg.txt
Enter a word to search for:nation
fourscore
'nation' is found 0 times in the file gettysburg.txt

答案 2 :(得分:0)

使用以下代码:

fname = input("Enter a file name to process:")
find = input("Enter a word to search for:")
text = open(fname, 'r').read()
def processone():
    if text is not None:
        words = text.lower().split()
        return words
    else:
        return None

def count_word(tokens, token):
    count = 0
    for element in tokens:
        word = element.replace(",", " ")
        word = word.replace("."," ")

        if word == token:
            count += 1
    return count

words = processone()

word = find
frequency = count_word(words, word)
print("'"+find+"'", "is found", str(frequency), "times in the file", fname)

声明“ return”为“ for”出去声明