我正在尝试使用python搜索文本文件并计算用户定义的单词出现的次数。但是当我在下面运行我的代码而不是得到文件中唯一单词出现次数的总和时,我得到一个计数,该文件中的数字行包含该单词。
示例:单词' bob'在文本文件中存在56次,出现在总共63行文本中的19行中。当我运行我的代码时,控制台会打印' 19'。
我猜我需要用我的分割方法做一些不同的事情?我正在运行Python 2.7.10。
user_search_value = raw_input("Enter the value or string to search for: ")
count = 0
with open(file.txt, 'r') as f:
for word in f.readlines():
words = word.lower().split()
if user_search_value in words:
count += 1
print count
答案 0 :(得分:0)
执行此操作的一种方法是在拆分行后循环显示单词并为每个匹配单词增加count
:
user_search_value = raw_input("Enter the value or string to search for: ")
count = 0
with open(file.txt, 'r') as f:
for line in f.readlines():
words = line.lower().split()
for word in words:
if word == user_search_value:
count += 1
print count
答案 1 :(得分:0)
正如我在上面的评论中提到的那样,在玩了这个(很长时间)之后,我发现了它。我的代码如下。
#read file
f = open(filename, "r")
lines = f.readlines()
f.close()
#looking for patterns
for line in lines:
line = line.strip().lower().split()
for words in line:
if words.find(user_search_value.lower()) != -1:
count += 1
print("\nYour search value of '%s' appears %s times in this file" % (user_search_value, count))
答案 2 :(得分:0)
如果"指定字符串"是一个带空格的短语,这是一个有用的短语:
#!/usr/bin/python
import sys
import os
def count_words_in_file(filepath, words, action=None):
with open(filepath) as f:
data = f.read()
for key,val in words.items():
print "key is " + key + "\n"
ct = data.count(key)
words[key] = ct
if action:
action(filepath, words)
def print_summary(filepath, words):
print(filepath)
for key,val in sorted(words.items()):
print('{0}:\t{1}'.format(
key,
val))
filepath = sys.argv[1]
keys = ["Hello how are you",
"Another phrase with spaces",
"A phrase with spaces and some punctuation."]
words = dict.fromkeys(keys,0)
count_words_in_file(filepath, words, action=print_summary)