Question

Writing a program in Python that reads a text file and organizes the words in the...

Writing a program in Python that reads a text file and organizes the words in the file into a list without repeating words and in all lowercase.

Here is what I have

#This program takes a user input file name and returns each word in a list
#and how many different words are in the program.
while True:   #While loop to loop program
    words = 0
    #list1 = ['Programmers','add','an','operating','system','and','set','of','applications','to','the','hardware',
         # 'we','end','up','with','a','Personal','Digital','Assistant','that','is','quite','helpful','capable',
          #'helping','us','do','many','different','things']
  
    try:
        fname = input('Please enter the file name to open:')
        fhand = open(fname)
        list1 = list()
      
  
        for line in fhand: #Iteration over each line
            words = line.split()
            list1.append(words)
            #words = [words.lower() for words in list1]
          
        print('list',list1)
        print('words',words)

        sorted_list = sorted(words)
        print(sorted_list)
        print('File',fname,'has',len(list1),'different words.')
                      
        ans = input("Do you want try again? y/n: ") #asking user to contiune
        
        if ans == 'y' or ans == 'Y':
            continue
        else:
            if ans == 'n' or ans == 'N':
                print('Goodbye')
                break   
    except:
        print('File cannot be opened:', fname)
        continue
           

Homework Answers

Answer #1

Program Code Screenshot :

Sample Output :

Program Code to Copy (Please refer to the screenshot of the code to understand the indentation of the code)

while True: #While loop to loop program
try:
#Open a file
fname = input('Please enter the file name to open: ')
fhand = open(fname)
#List to store all the words
list1 = []
#Open all lines in the file
for line in fhand: #Iteration over each line
words = line.split()
#Add to list
list1.extend([word.lower() for word in words])
  
print('list',list1)
#Print count of different words
print('File',fname,'has',len(set(list1)),'different words.')
#Prompt again
ans = input("Do you want try again? y/n: ") #asking user to contiune
if ans == 'y' or ans == 'Y':
continue
else:
if ans == 'n' or ans == 'N':
print('Goodbye')
break   
except:
print('File cannot be opened:', fname)
continue

Know the answer?
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
Similar Questions
Please write a program that reads the file you specify and calculates how many times each...
Please write a program that reads the file you specify and calculates how many times each word stored in the file appears. However, ignore non-alphabetic words and convert uppercase letters to lowercase letters. For example, all's, Alls, alls are considered to be the same words. What is the output of the Python program when the input file is specified as "proverbs.txt"? That is, in your answer, include the source codes of your word counter program and its output. <proverbs.txt> All's...
I need python code for this. Write a program that inputs a text file. The program...
I need python code for this. Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. Uppercase words should take precedence over lowercase words. For example, 'Z' comes before 'a'. The input file can contain one or more sentences, or be a multiline list of words. An example input file is shown below: example.txt the quick brown fox jumps over the lazy dog An example of the program's output...
Implement a python program in file named tarvel.py. Create an empty dictionary named responses. Implement while...
Implement a python program in file named tarvel.py. Create an empty dictionary named responses. Implement while loop to take in user's name and desired destination for as long as there are user inputs. Prompt user to input yes to continue and no to quit. Prompt for user's name. Receive the name into the program and save it as the value of name variable. Prompt user for their desired vacation destination. Receive response and save it as the value of a...
Attached is a text file full of names. Write a program that prompts the user to...
Attached is a text file full of names. Write a program that prompts the user to type in a name. If the name appears in the list of names, the program prints a message indicating the name was found. If the name entered by the user is not in the database, the program prints a different message indicating the name was not found. The program will continue prompting the user and searching for names until the user enters "quit". The...
A file concordance tracks the unique words in a file and their frequencies. Write a program...
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 input file name:...
So, i have this code in python that i'm running. The input file is named input2.txt...
So, i have this code in python that i'm running. The input file is named input2.txt and looks like 1.8 4.5 1.1 2.1 9.8 7.6 11.32 3.2 0.5 6.5 The output2.txt is what i'm trying to achieve but when the code runs is comes up blank The output doc is created and the code doesn't error out. it should look like this Sample Program Output 70 - 510, [semester] [year] NAME: [put your name here] PROGRAMMING ASSIGN MENT #2 Enter...
Write a Java program that reads words from a text file and displays all the words...
Write a Java program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. 1. You must use one of following Concrete class to store data from the input.txt file. Vector, Stack, ArrayList, LinkedList 2. To sort those words, you should use one of existing interface methods available in Collection or List class.
Python (Using for reference please comment which is which!) Exercise 1: Store Unique Words as Dictionary...
Python (Using for reference please comment which is which!) Exercise 1: Store Unique Words as Dictionary Keys Write a program that stores unique words as keys in a dictionary. Don't worry about upper/lowercase for now (i.e., it's ok for you to have separate entries for "The" and "the"), and just assign 1 as the value for each key. Use an input loop (while loop with an input function) to accepts values putting them into the dictionary until the user enters...
Write a python program that prompts the user for the names of two text files and...
Write a python program that prompts the user for the names of two text files and compare the contents of the two files to see if they are the same. If they are, the scripts should simply output “Yes”. If they are not, the program should output “No”, followed by the first lines of each file that differ from each other. The input loop should read and compare lines from each file. The loop should break as soon as a...
Create a program that filters the data in a CSV file of product data based on...
Create a program that filters the data in a CSV file of product data based on some search word and prints the resulting output to a new file. Additionally, the program will print the number of items filtered to stdout. • Your program should take three arguments: an input file to process, an output file to save the results, and a search word. • If the output file already exists or cannot be opened, warn the user by printing "CANNOT...