Question

Need some help on this Python Problem called "unique words" the text.txt file can be any...

Need some help on this Python Problem called "unique words" the text.txt file can be any .txt file with words in it.

Write a program that opens a specified text file and then displays the number of unique words in the text file after stripping all the white spaces, comma, and the punctuation marks and list of all the unique words found in the file. The list should be sorted in alphabetical order. The program should handle the ‘file not found’ error. Store each word as an element of list. Be sure not to use the function ‘set’ in the program.

Sample outputs

Enter the name of the input file:

Error: File not found!

Another run….

Enter the name of the input file: text.txt

There are 171 unique words in the text.

These are the unique words in the text sorted in alphabetical order:

1840

2,000

According

Arabia

China

Whose

Wheels

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Please uprate and please comment for any further help.

Code Snapshot:

Code:

import re
def main():
    list = []
    file=input("Enter the name of the input file: ")
    try:
        f = open(file)
    except IOError:
        print('Error: File not found!')
    else:
        with f:
            fileData=f.readlines()
        for x in fileData:
            x=re.sub(r'([^\w\s]|_)+(?=\s|$)', '', x) #to remove extra special character
            words=x.split(" ")
            for word in words:
                word=word.strip() #removing spaces
                if word: #checking if word is not empty
                    if word not in list:
                        list.append(word)      
      
        list.sort() # sorting the list
        print("There are "+str(len(list))+" unique words in the text.") #printing the length
        print("These are the unique words in the text sorted in alphabetical order:")
        for word in list:
            print(word) #printing word by word

main()

Output:

Add a comment
Know the answer?
Add Answer to:
Need some help on this Python Problem called "unique words" the text.txt file can be any...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python: The attached text file: https://drive.google.com/open?id=1Hcj4Ey4NbKHn5-vyF-foR3iny0aX8y1...

    Python: The attached text file: https://drive.google.com/open?id=1Hcj4Ey4NbKHn5-vyF-foR3iny0aX8y1c Unique Words Write a program that opens a specified text file and then displays the number of unique words in the text file after stripping all the white spaces, comma, and the punctuation marks and a list of all the unique words found in the file. The list should be sorted in alphabetical order. The program should handle the ‘file not found’ error. Store each word as an element of list. Be sure not...

  • Python 3:Write a program that inputs a text file. The program should print the unique words...

    Python 3:Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order.   Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order An example file along with the correct output is shown below: example.txt the quick brown fox jumps over the lazy dog Enter the input file name: example.txt brown dog fox jumps lazy over quick the

  • Homework IX-a Write a program that opens a text file, whose name you enter at the keyboard You wi...

    Homework IX-a Write a program that opens a text file, whose name you enter at the keyboard You will be using the file text.txt to test your program. Print out all of the individual, unique words contained in the file, in alphabetical order Print out the number of unique words appearing in text.txt. Call your program: YourName-HwrklXa.py Make sure that your name appears as a comment at the beginning of the program as well as on the output display showing...

  • Programming Exercise 5.8 A file concordance tracks the unique words in a file and their frequencies....

    Programming Exercise 5.8 A file concordance tracks the unique words in a file and their frequencies. Write a program that displays a concordance for a file. The program should output the unique words and their frequencies in alphabetical order. Variations are to track sequences of two words and their frequencies, or n words and their frequencies. Below is an example file along with the program input and output: example.txt I AM SAM I AM SAM SAM I AM Enter the...

  • I cant get this python program to read all the unique words in a file. It...

    I cant get this python program to read all the unique words in a file. It can only read the number of the unique words in a line. import re import string from collections import Counter # opens user inputted filename ".txt" and (w+) makes new and writes def main(): textname = input("Enter the file to search: ") fh = open(textname, 'r', encoding='utf-8' ) linecount = 0 wordcount = 0 count = {} print("Sumary of the", fh) for line in...

  • write a python program that prompts the user for a name of a text file, opens...

    write a python program that prompts the user for a name of a text file, opens that file for reading , and tracks the unique words in a file and counts how many times they occur in the file. Your program should output the unique words and how often they occur in alphabetical order. Negative testing for a file that does not exist and an empty file should be implemented.

  • Write a program that opens a specified text file and then displays a list of all...

    Write a program that opens a specified text file and then displays a list of all the unique words found in the file. Hint: Store each word as an element of a set. USING PYTHON Please provide a screenshot of the input

  • Write a Python program to read lines of text from a file. For each word (i.e,...

    Write a Python program to read lines of text from a file. For each word (i.e, a group of characters separated by one or more whitespace characters), keep track of how many times that word appears in the file. In the end, print out the top twenty counts and the corresponding words for each count. Print each value and the corresponding words, in alphabetical order, on one line. Print this in reverse sorted order by word count. You can assume...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • Python program - Write a Python program, in a file called sortList.py, which, given a list...

    Python program - Write a Python program, in a file called sortList.py, which, given a list of names, sorts the names into alphabetical order. Use a one dimensional array to hold the list of names. To do the sorting use a simple sorting algorithm that repeatedly takes an element from the unsorted list and puts it in alphabetical order within the same list. Initially the entire list is unsorted. As each element is placed in alphabetical order, the elements in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT