Open the file romeo.txt and read it line by line. For each line, split the line into a list of words using the split() method. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort and print the resulting words in python sort() order as shown in the desired output.
Question
Open the file romeo.txt and read it line by line. For each line, split the line into a list of words using the split() method. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort and print the resulting words in python sort() order as shown in the desired output.
Solution
Here is a step-by-step guide on how to accomplish this task in Python:
- Open the file using the
open()function. This function takes two parameters: the name of the file you want to open and the mode in which you want to open it. In this case, you want to open the file in read mode, so you would use'r'as the second parameter.
file = open('romeo.txt', 'r')
- Read the file line by line using a for loop. In Python, a file object is also an iterable object, which means you can use it in a for loop and it will return its lines one by one.
for line in file:
- Split each line into a list of words using the
split()method. This method splits a string into a list where each word is a separate element in the list.
words = line.split()
- Check if each word is already in the list. If it's not, append it to the list. You can do this using an if statement and the
not inoperator.
for word in words:
if word not in word_list:
word_list.append(word)
- After the for loop, sort the list using the
sort()method. This method sorts the elements of a list in a specific order - either ascending (by default) or descending.
word_list.sort()
- Finally, print the sorted list.
print(word_list)
Here is the complete code:
file = open('romeo.txt', 'r')
word_list = []
for line in file:
words = line.split()
for word in words:
if word not in word_list:
word_list.append(word)
word_list.sort()
print(word_list)
This code will open the file romeo.txt, read it line by line, split each line into a list of words, add each word to a list if it's not already in it, sort the list, and print it.
Similar Questions
Write a Python program that sorts all lines alphabetically within a text file.The program will be given 2 command line arguments: the first specifies the path to the file to sort, and the second specifies the column number to use as the key for sorting the file.Columns are numbered starting from 1. You can assume columns in the file are separated by a space " ", and that the column number provided is a valid column in the file.If the value in one column is a duplicate, the program sorts the lines with the duplicate value in alphabetical order based on the contents of their entire lines from start to finish.You should write your solution in sort.py.
Johnny is developing a tool that manipulates the text files efficiently. Your task is to assist johnny to create a Python program that reads a text file (InputData1.txt) and prints each line in reverse order. Sample Test Cases Test Case 1: Expected Output: .ylgu·naht·retteb·si·lufituaeB .ticilpmi·naht·retteb·si·ticilpxE .xelpmoc·naht·retteb·si·elpmiS .detacilpmoc·naht·retteb·si·xelpmoC .detsen·naht·retteb·si·talF .esned·naht·retteb·si·esrapS .stnuoc·ytilibadaeR
Count the sentences, words and characters in a fileWrite a Python program that reads a file and prints the number of lines, words, and characters in the file.Input Format:The input will be a single string, which is the name of the file to be read.Output Format:The output will be three separate lines, each containing a count:The first line will contain the number of lines in the file, prefixed with "Sentences: ".The second line will contain the number of words in the file, prefixed with "Words: ".The third line will contain the number of characters in the file, prefixed with "Chars: ".Sample Test CasesTest Case 1:Expected Output:file·name:input1.txtSentences:·8Words:·34Chars:·228Test Case 2:Expected Output:file·name:input2.txtSentences:·7Words:·51Chars:·306
InstructionsIn this task, you should define two sorting algorithms that you can use to sort your lexicon of words. In the next task you will write a short program to use one of the sorting algorithms to sort your lexicon. Following sorting, words in your lexicon should be sorted in alphabetical order.It is entirely up to you which two sorting algorithms you include, as long as they are in the subject and different regarding time complexity.You should write one function per-sorting algorithm. Each of these functions should take a single parameter, the lexicon to be sorted. They should sort the lexicon in-place, so they do not need to return anything.
Write a function in python to read the content from a text file "poem.txt" line by line and display the same on screen. Solution
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.