Python | Count the occurrences of each word in a given text file (using a dictionary)



Example # 1:
First we create a text file from which we want to count words. Let this file be sample.txt with the following content:

 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry  

Note. Make sure the text file is in the same directory as the Python file.

# Open file in reading mode

text = open ( "sample.txt" , "r" )

 
# Create empty dictionary

d = dict ()

  
# Step through each line in the file

for line in text:

# Remove spaces and newlines

line = line.strip ()

 

# Convert characters in the string to

# lowercase to avoid case mismatch

line = line.lower ()

 

  # Split string into words

  words = line.split ( " " )

 

# Loop over each word in line

for word in words:

# Check if the word is in the dictionary

  if word in d:

  # Increase word count by 1

d [word] = d [word] + 1

else :

# Add word to dictionary with number 1

d [word] = 1

 
# Print the contents of the dictionary

for key in list (d.keys ()):

print (key, ":" , d [key])

Exit :

 mango: 3 banana: 3 apple: 3 pear: 2 grapes: 1 strawberry: 2 kiwi: 1 

Example # 2:

Consider the file sample.txt which has punctuation sentences .

 Mango! banana apple pear. Banana, grapes strawberry. Apple- pear mango banana. Kiwi "apple" mango strawberry. 

import string

 
# Open file in read mode

text = open ( "sample.txt" , "r" )

 
# Create empty dictionary

d = dict ()

 
# Step through each line of the file

for line in text:

  # Remove spaces and newlines

line = line.strip ()

  

# Convert characters in the string to

# lowercase to avoid case mismatch

line = line.lower ()

 

# Remove punctuation marks from line

line = line.translate (line.maketrans (" ", " ", string.punctuation))

  

  # Split string into words

words = line.split ( "" )

  

  # Loop over each word in the line

for word in words:

  # Check if the word is in the dictionary

  < code class = "keyword"> if word in d:

# Increase word count by 1

d [word] = d [word ] + 1

  else :

# Add word to dictionary with number 1

d [word] = 1

 
# Print the contents of the dictionary

for key in list (d.keys ()):

print (key, " : " , d [key])

Exit:

 mango: 3 banana: 3 apple: 3 pear: 2 grapes: 1 strawberry: 2 kiwi: 1