Ryan
Ryan

Reputation: 33

Search text file and count occurrences of a specified string

I am attempting to use python to search a text file and count the number of times a user defined word appears. But when I run my code below, instead of getting a sum of the number of times that unique word appears in the file, I am getting a count for the number lines within that file contain that word.

Example: the word 'bob' exists 56 times in the text file, appearing in 19 of the total 63 lines of text. When I run my code the console prints '19'.

I am guessing I need to do something different with my split method?

user_search_value = 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)

Upvotes: 1

Views: 13278

Answers (3)

user94559
user94559

Reputation: 60153

One way to do this would be to loop over the words after you split the line and increment count for each matching word:

user_search_value = 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)

Upvotes: 0

Ryan
Ryan

Reputation: 33

I figured it out. My code is below.

#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))

Upvotes: 0

nettie
nettie

Reputation: 692

If the "specified string" is a phrase with spaces, here is one that works:

#!/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)

Upvotes: 0

Related Questions