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.

if(word[-1].isalnum()!=True):
    temp=""
    for i in word:
        if(i.isalnum()):
            temp=temp+i
    word=temp

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

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

Repeat the above steps for each line in the file.

Text file:

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

Output:

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

Download project

Reviews Report

Submitted by Avani Vartak (avanivartak)

Download packets of source code on Coders Packet