Question

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
0 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

Code Screenshots:

#Prompt the user to enter the file name. file_nameinput (Enter the input file name:) Open the file file_obj open (file_name

Sample Input File(“example.txt”):

the quick brown fox jumps over the lazy dog

Sample Output:

:i.or)ui ; :; : :..1.42 く:1Kismip.1.42..tari; ytri;42r i;he: example.txt tAi 2mx ; :: brown dog fox jumps lazy over quick the

Code To Copy:

#Prompt the user to enter the file name.

file_name = input("Enter the input file name: ")

#Open the file.

file_obj = open(file_name)

#Define a list to store the unique words in the file.

unique_words = []

#Run the loop to traverse each line in the file.

for line in file_obj:

    #Obtain the list of words on the current line.

    words = line.split()

   

    #Run the loop to access each word on the current line.

    for curr_word in words:

        #If there are any punctuations in

        # the word then, remove them.

        curr_word = curr_word.strip(',.?!')

        #If the current word is not present in

        #the list of the unique words then,

        #add it to the list.

        if curr_word not in unique_words:

            unique_words.append(curr_word)

#Close the file.

file_obj.close()

#Sort the list containing the unique words.

unique_words.sort()

#Display an empty line on the console.

print()

#Runthe loop to display the unique words.

for curr_word in unique_words:

    print(curr_word)

Add a comment
Answer #2

It's still giving me an error for Program output test 1 and Program output test 2.


source: python
answered by: Katrina
Add a comment
Know the answer?
Add Answer to:
Python 3:Write a program that inputs a text file. The program should print the unique words...
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
  • Please solve. How do I create my own text file? Where do I save it so...

    Please solve. How do I create my own text file? Where do I save it so that it is recognized by python? Do I have to import it into my code? the prompt: 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:...

  • Write a program that inputs a text file. The program should print the unique words in...

    Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. Note: The sort function sorts first numbers, then capital letters, then lowercase letters. That is fine for this assignment as you can see from the sample output. But even if the same word appears multiple times in the file it should only be displayed once in the output. (python)

  • -Brief description of the program: - input(s) and output(s) - brief description or relationship b...

    -Write in C++ language. -Brief description of the program: - input(s) and output(s) - brief description or relationship between inputs and outputs Submission Instructions: Submit only.c files in the designated Dropbox on D2L Problem 1 (25 points): Write a C program that a) reads one line (sentence) at a time from a text file called inputtext.txt (on D2L), b) reverses the words on that line, c) prints (on the screen) the modified line and d) write the modified line in...

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

  • Simple Python Program Working with strings Write a Python program to read in a number of...

    Simple Python Program Working with strings Write a Python program to read in a number of strings from the user. Stop when the user enters “Done”. Then check if each of the strings contains all the letters of the alphabet. Print the results in the form of a dictionary, where they keys are the strings and the values are the Truth Value for the string, which you just calculated. Sample Run: Enter the strings: taco cat The quick brown fox...

  • I/O program for C Write a program in direct1.c which reads from standard input a line...

    I/O program for C Write a program in direct1.c which reads from standard input a line and then outputs that line immediately to standard output. it should read the first line only ! Then, write another program called direct2.c which reads from standard input every line until end of input and outputs them to standard output. Everything that goes in should come out exactly as it was. Compile both programs and run them on the input files given below 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.

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

  • Write a program using regular expression to find the starting and ending locations of ‘fox’ in...

    Write a program using regular expression to find the starting and ending locations of ‘fox’ in the text 'The quick brown fox jumps over the lazy dog.' Then print out its locations as follows: Found fox from (the starting location) to (the ending location).

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

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