Hi, in this blog lets learn Python Program to Count The Frequency of Words in a File. Given a text file containing words and lines, you should try writing a program to count words frequency of occurrence and print it.
This program is divided into two phases
1) Open the file in read mode, get the data, clean the data or any special characters.
2) Split the data and frame dictionary having word as key and frequency as value. Count occurrence of each word and increment value and print it.
Phase 1: Open file and read the contents
Phase 2: Split the data and frame dictionary have frequency as value
Run 1 :
Contents of sample.txt
Write a Python Program to Count The Frequency of Words in a File
OUTPUT :
Write occurs : 1 times
a occurs : 2 times
Python occurs : 1 times
Program occurs : 1 times
to occurs : 1 times
Count occurs : 1 times
The occurs : 1 times
Frequency occurs : 1 times
of occurs : 1 times
Words occurs : 1 times
in occurs : 1 times
File occurs : 1 times
Conclusion : Try to run the program by yourself and comment down below if you have any issue.
0 Comments