Question

•Write use pytohn a function File2List(filename) that: –Read line by line from a file –For each...

•Write use pytohn a function File2List(filename) that:

–Read line by line from a file

–For each line of words,

–change all characters to lowercase

–remove all the punctuations

–remove whitespaces except the blank – ‘ ’

–split the words within the line by the blank and form a list of separated words

–Merge all the list of separated words together and the function should return a wordlist of individual words

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you like the solution please give it a thumbs up!

Solution :-

Python Code :-

# function returns resultant list

def File2List(fileName):

file = open(fileName, "r")

#list to store final merged lists

Result = []

# read the file line by line

f = file.readlines()

for x in f:

print("\ncurrent line :-\n" + x)

temp = x

temp = temp.lower() #lower the string characters if any

for ch in x:

# check for any punctuation and remove it from the line

if (not(ch.isalpha()) and not(ch.isdigit()) and not(ch.isspace())):

temp = temp.replace(ch,'',1)

#split the line and store in list

List = temp.split()

print("current List :- ")

print(List)

#merge list with the resultant list

Result = Result + List

return Result

List = File2List("input.txt")

#print resultant list

print("\nResultant list :-")

print(List)

Input file input.txt :-

Python can be easy to pick up, whether you're a first time programmer or you are
experienced with other languages.
The following pages are a useful first step
to get on your way writing programs with Python!

Screenshot for the code and sample input/output :-

Add a comment
Know the answer?
Add Answer to:
•Write use pytohn a function File2List(filename) that: –Read line by line from a file –For each...
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
  • C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the r...

    C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the required information. Your function should return the number of student entries read from the file. Empty lines do not count as entries, and should be ignored. If the input file cannot be opened, return -1 and do not print anything.     Your function should be named...

  • 12. (10 pts) Fill in the blanks according to the documentation: def readAndStrip (fileName, charsToRemove) :...

    12. (10 pts) Fill in the blanks according to the documentation: def readAndStrip (fileName, charsToRemove) : ""This function will take in a fileName and characters to remove. It will read everything from the fileName and return a lis of all the words with given chars ToRemove removed.""" #Open the file file = #Read the file #Close the file #Create the list #Strip all the charsToRemove for #return the clean list return

  • python Write the function getSpamLines(filename) that read the filename text file and look for lines of the form &#...

    python Write the function getSpamLines(filename) that read the filename text file and look for lines of the form 'SPAM-Confidence: float number'. When you encounter a line that starts with "SPAM-Confidence:" extract the floating-point number on the line. The function returns the count of the lines where the confidence value is greater than 0.8. For example, if 'spam.txt' contains the following lines along with normal text getSpamLines('spam.txt') returns 2. SPAM-Confidence: 0.8945 Mary had a little lamb. SPAM-Confidence: 0.8275 SPAM-Confidence: 0.7507 The...

  • # DISCUSSION SECTION WORK: # # 1. STUDENTS: download this file, ds4.py, and wordsMany.txt, from #...

    # DISCUSSION SECTION WORK: # # 1. STUDENTS: download this file, ds4.py, and wordsMany.txt, from # http://www.cs.uiowa.edu/~cremer/courses/cs1210/etc/ds4/ # Save both in the same folder. # # 2. TA (aloud) and STUDENTS: Read the comments from START HERE! (just after these instructions) # to definition of anagramInfo function. Discuss any questions about what the functions should do. # # 3. TA demonstrate running anagramInfo("wordsMany.txt") on this unchanged file, to # see that it behaves reasonably despite having incomplete anagram-testing functions. #...

  • Capitalization JAVA In this program, you will read a file line-by-line. For each line of data...

    Capitalization JAVA In this program, you will read a file line-by-line. For each line of data (a string), you will process the words (or tokens) of that line one at a time. Your program will capitalize each word and print them to the screen separated by a single space. You will then print a single linefeed (i.e., newline character) after processing each line – thus your program will maintain the same line breaks as the input file. Your program should...

  • Write a function named "loadStateDict(filename) that takes a filename and returns a dictionary of 2-character state...

    Write a function named "loadStateDict(filename) that takes a filename and returns a dictionary of 2-character state codes and state names. The file has four columns and they are separated by commas. The first column is the state full name and the second column is the state code. You don't have to worry about column 3 & 4. You should eliminate any row that is without a state code. Save the two columns into a dictionary with key = state code...

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

  • QUESTION17 The following Python script aims to read lines from a text file and add them...

    QUESTION17 The following Python script aims to read lines from a text file and add them to a list Marlout oft. 00 Example P Flag question If the text file contains the following lines wordt wordz words the list will be ['word, word2, Words' after the execution of the program Question: Rearrange the following statements on the correct (logical) form Note: The-indicates the indentation #--> Program infile=onen/fileName --infile = open(fileName, 'r') . . -try: --for line in infile: --infile =...

  • Create a C++ program that will read a series of titles from an input file (Do...

    Create a C++ program that will read a series of titles from an input file (Do not use fstream), one per line. The titles will be in an unorthodox mix of upper and lowercase letters. Reformat each title so that the first letter of each word is capitalized and all remaining letters are lowercase. For example, "thE CAT in tHe hat" becomes "The Cat In The Hat". Assume there are no leading spaces before the first word of each title...

  • Write a C program to run on ocelot to read a text file and print it...

    Write a C program to run on ocelot to read a text file and print it to the display. It should optionally find the count of the number of words in the file, and/or find the number of occurrences of a substring, and/or take all the words in the string and sort them lexicographically (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring]...

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