Change language

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 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically