0% found this document useful (0 votes)
163 views

Python Program To Count The Frequency of Words From A Text File

A Simple python program to count the frequency of any word from a text file stored on your computer.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
163 views

Python Program To Count The Frequency of Words From A Text File

A Simple python program to count the frequency of any word from a text file stored on your computer.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

# Python program to count the frequency of words

# from a file..

fh = open ("demo2.txt","r") #File Handler

cont = fh.read() #Content stored in string format into cont variable

lst = cont.split()

#Each word of the content stored into

#the variable lst in list format

#(note: each word is string but stored into list lst)

nwlst = []

# Created new list to store the words of the old list

# after converting them to uppercase

for j in range(len(lst)):

nwlst.append(lst[j].upper())

# converting each of the list 'lst' to uppercase and,

# storing them to new list 'nwlst'

eplst = []

# Created new list to pass counted variable into it

# So, that there is no same frequency displayed twice or more.

for i in nwlst: # Picking each word from the capitalized list.

if i not in eplst: # Checking if its already have beem counted.

c = nwlst.count(i) # Counting the frequency of the words.

print("Frequency of ",i," = ",c) # Printing the frequency of the worfs.

eplst.append(i) # storing the counted words into the list.


# NOTE: Use of eplst has been described above.

fh.close() #closing the File Handler

You might also like