Coders Packet

Unique words from a file using Python

By Avani Vartak

This Python packet is about finding and printing unique words from a text file using python.

In this project, we will learn how to find unique words in a text file using Python.

To find unique words in a text file, you must first find the words, then clean the words (remove punctuation marks), and then finally find the unique words.

1. Fetch line from file and split the line into words using the split() function.

word_list = line.split()

2. Remove punctuation marks from words if any.

    for i in word:

3. If the words aren't there in the list, append them to the list.

if(word.lower() not in final_list):

Repeat the above steps for each line in the file.

Text file:

Let it snow, let it snow, let it snow.


Enter file name: sample1.txt
Unique word count is 3 and words are :
['it', 'let', 'snow']

Download Complete Code


No comments yet