Sn0w3y
Sn0w3y

Reputation: 17

Count how often a Text is given in an txt File Python

how is it possible for me to count the word "OFFLINE" in the Following List with Python? The List is in a .txt File so i need to open that first i guess and then look after the specific word.

MPP MPP Fault MPP Fault MPP MPP MPP MPP MPP MPP MPP MPP MPP MPP MPP MPP MPP MPP MPP MPP MPP MPP MPP MPP OFFLINE OFFLINE OFFLINE OFFLINE OFFLINE OFFLINE OFFLINE OFFLINE OFFLINE OFFLINE OFFLINE OFFLINE OFFLINE OFFLINE

The values as under each another.

I already tried this:

    def word_count(str):
    counts = dict()
    words = str.split()

    for word in words:
        if word in counts:
            counts[word] += 1
        else:
            counts[word] = 1

    return counts

print( word_count(open(info).read())) 

Upvotes: 0

Views: 44

Answers (4)

Gábor Fekete
Gábor Fekete

Reputation: 1358

from collections import Counter

c = Counter(text.split())
print(c)
print(c['OFFLINE'])

Use the built-in Counter class to count all words. Counter is inherited from dict with some additional methods.

Outputs:

Counter({'MPP': 23, 'OFFLINE': 14, 'Fault': 2})
14

Upvotes: 0

Scasto
Scasto

Reputation: 32

You can use the count() function for this. My example .txt file has the following contents:

MPP Fault offline offline OFFLINE MPP 

This is my Script:

file = open("test.txt", "r") 
data = file.read()
word = "offline"
occurrences = data.count(word)
print('Number of occurrences of the word :', occurrences)

Output:

Number of occurrences of the word : 2

Note that the count() function is case-sensitive. So it did not count "OFFLINE" but only "offline". If I want it to be case-insensitive I can add a transformation to my string beforehand with the upper() function.

occurrences = data.upper().count(word.upper())

Output:

Number of occurrences of the word : 3

I retrieved the example from: https://pythonexamples.org/python-count-occurrences-of-word-in-text-file/

Upvotes: 0

0x0fba
0x0fba

Reputation: 1620

def word_count(str):
    return str.split().count("OFFLINE")

If the goal is to count the number of OFFLINE occurences, you don't need a dict.

def word_count(str):
    counts = 0
    words = str.split()
    for word in words:
        if word == "OFFLINE":
            counts += 1
    return counts

Upvotes: 1

Aymen
Aymen

Reputation: 839

Following your approach:

You need to send the word that you are looking for as parameter:

text = "MPP MPP Fault MPP Fault MPP MPP MPP MPP MPP MPP MPP MPP MPP MPP MPP MPP MPP MPP MPP MPP MPP MPP MPP MPP OFFLINE OFFLINE OFFLINE OFFLINE OFFLINE OFFLINE OFFLINE OFFLINE OFFLINE OFFLINE OFFLINE OFFLINE OFFLINE OFFLINE"

def word_count(str, your_word):
    counts = dict()
    words = str.split()

    for word in words:
        if word in counts:
            counts[word] += 1
        else:
            counts[word] = 1

    return counts[your_word]

print(word_count(text, 'OFFLINE'))

Second solution:

print(text.count('OFFLINE'))

Upvotes: 0

Related Questions