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
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...
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...
c# please Create a “Main” program which reads a text file called “collectionOfWords.txt”. Include exception handling...
c# please Create a “Main” program which reads a text file called “collectionOfWords.txt”. Include exception handling to check if the file exists before attempting to open it. Read and print each string to the console. Next modify each word such at the first letter in each word is uppercase and all other letters are lowercase. Display the modified word on the console. Creating a text file: Open up Notepad and type in the following words. Save the file as collectionOfWords.txt...
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.
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...
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...
Python: Simple Banking Application Project Solution: • Input file: The program starts with reading in all...
Python: Simple Banking Application Project Solution: • Input file: The program starts with reading in all user information from a given input file. The input file contains information of a user in following order: username, first name, last name, password, account number and account balance. Information is separated with ‘|’. o username is a unique information, so no two users will have same username. Sample input file: Username eaglebank has password 123456, account number of BB12 and balance of $1000....
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
The following code to run as the described program on python. The extra test file isn't...
The following code to run as the described program on python. The extra test file isn't included here, assume it is a text file named "TXT" with a series of numbers for this purpose. In this lab you will need to take a test file Your program must include: Write a python program to open the test file provided. You will read in the numbers contained in the file and provide a total for the numbers as well as the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT