计算文本文件中的所需单词
Counting a desired word in a text file
我必须计算给定文本文件中给定单词出现的次数,这个是葛底斯堡演说。出于某种原因,它没有计算我输入的 'nation' 所以输出看起来是这样的:
'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)
我的第一个函数将文件拆分为一个字符串并将所有字母转为小写。第二个删除标点符号并且应该计算输入中给出的单词。
以我的第一个编码 class 为例,如果您发现我的编码中有更多缺陷或可以进行改进,以及帮助找到我的问题的解决方案,请随意。
在 count_word()
函数的 for
循环中,循环末尾有一个 return
语句,它仅在一次循环迭代后立即退出函数。
您可能希望将 return
语句移动到 for
循环之外。
作为初学者,我建议您使用打印语句并查看正在打印的变量,这有助于解决问题。例如,print 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
使用下面的代码:
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"
我必须计算给定文本文件中给定单词出现的次数,这个是葛底斯堡演说。出于某种原因,它没有计算我输入的 'nation' 所以输出看起来是这样的:
'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)
我的第一个函数将文件拆分为一个字符串并将所有字母转为小写。第二个删除标点符号并且应该计算输入中给出的单词。
以我的第一个编码 class 为例,如果您发现我的编码中有更多缺陷或可以进行改进,以及帮助找到我的问题的解决方案,请随意。
在 count_word()
函数的 for
循环中,循环末尾有一个 return
语句,它仅在一次循环迭代后立即退出函数。
您可能希望将 return
语句移动到 for
循环之外。
作为初学者,我建议您使用打印语句并查看正在打印的变量,这有助于解决问题。例如,print 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
使用下面的代码:
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"