Question

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 to use the function ‘set’ in the program.

Sample Outputs(s):

Enter the name of the input file: text
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:
1,162
1,820
1840
1853
1867
2,000
400-horsepower
650-horsepower
Accordingly
Arabia
Atlantic
China Co
....
....
understand
undertaking
uproar
vessels
voyage
were
wheels
whose
will
with
without
wooden
world
years

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:

kAAAAASUVORK5CYII=

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:

In [20]: runfile(C:/Users/Computer Science/Desktop/Python/UniqueFile/ listUniqueFiles.py,wdir- C:/Users/Computer Science/De

Console 1/A 400-horsepower 650-horsepower Accordingly Arabia Atlantic China 5 Cunard Eastern Eight English France Given Great

wPcwj2zlV94OQAAAABJRU5ErkJggg==

Add a comment
Know the answer?
Add Answer to:
Python: The attached text file: https://drive.google.com/open?id=1Hcj4Ey4NbKHn5-vyF-foR3iny0aX8y1...
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
  • 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...

  • 1. Expense Pie Chart Create a text file that contains your expenses for last month in...

    1. Expense Pie Chart Create a text file that contains your expenses for last month in the following categories: -Rent -Gas -Food -Clothing -Car Payment -Misc Write a Python program that reads the data from the file and uses matplotlib to plot a pie chart showing you how you spend your money. 2. 1994 weekly gas graph The text file named 1994_Weekly_Gas_Averages.txt contains the average gas price for each week in the year 1994. (There are 52 lines in 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...

  • 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

  • 1. Please provide code based on Chapter 12 only from the text. Also include output to...

    1. Please provide code based on Chapter 12 only from the text. Also include output to console picture after the program is compiled. 2. Text is Tony Gaddis 9th edition C++. Chapter 12, page 714, Problem 5 'Line Numbers' 3. Please use 'forChapt12.txt' file for the problem. The text is pasted below after description of the problem. 5. LINE NUMBERS (page 714) Write a program that asks the user for the name of the file. The program should display the...

  • 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.

  • (Python 3) Write a program that reads the contents of a text file. The program should...

    (Python 3) Write a program that reads the contents of a text file. The program should then create a dictionary in which the keys are individual words found in the file and the values are the number of times each word appears and a list that contains the line numbers in the file where the word (the key) is found. Then the program will create another text file. The file should contain an alphabetical listing of the words that are...

  • 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...

  • Write a complete Python program with prompts for the user for the main text file (checks...

    Write a complete Python program with prompts for the user for the main text file (checks that it exists, and if not, output an error message and stop), for any possible flags (including none), and for any other input that this program may need from the user: split has an option of naming the smaller files head_tail list the first 10 lines (default) and the last 10 lines (default) in order of the given text file flag: -# output #...

  • 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

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