(1) Write a program to read a text file and find unique words with their occurrences anddisplay tabular form the analysis. code example
Example 1: count the frequency of words in a file
from collections import Counter
def word_count(fname):
with open(fname) as f:
return Counter(f.read().split())
print("Number of words in the file :",word_count("test.txt"))
Example 2: Python Program to Count the Occurrences of a Word in a Text File
file = open("C:\workspace\python\data.txt", "r")
data = file.read()
occurrences = data.count("python")
print('Number of occurrences of the word :', occurrences)