Question

The following shows the result of calling function read_from_file_sum_squares() for each of these two files: >>> from praco iusing python

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

Hello!

# is used comment in python

list is the collection which is changable similar to array

map is used to map all the elements to the list variable

input() is used to get input in string format

split() is used to split all the elemets

rstrip() is used to strip '\n' and spaces

And i have explained more clearly using comments.

CODE:

def read_from_file_table():
        # declare list of lists
        list_of_lists=[]
        # asks the user to enter file name
        fname=input("Enter the filename:")+".txt"
        # open file for read only
        f=open(fname,"r")
        # reads all the lines in the file
        filedata=f.readlines()
        # iterates over each line
        for eachline in filedata:
            # strips new line and splits all the numbers
            line=eachline.rstrip().split()
            # creates a list with empty elememts
            list_for_each_line=[]
            # iterates over every element in the line
            for number in line:
                # if the number is greater than 0 
                if(int(number)>=0):
                    # then only it wll append
                    list_for_each_line.append(int(number))
            # finally appens the list of the line to list of lists
            list_of_lists.append(list_for_each_line)
        # prints the list of lists
        print(list_of_lists)

# this code helps us to not let run immediately after import
# whenever function is called only it will executed
if __name__ == "__main__":
    read_from_file_table()

praco.py - C:/Users/DELL/Desktop/New folder/praco.py (3.8.3) File Edit Format Run Options Window Help def read from file tabl

TEXT FILE:

fIle1.txt

1 2 3
4 5
6

-8 -9 99
33

Luahan La file1 - Notepad File Edit Format View Help 1 2 3 4 5 6 -8 -9 99 33

OUTPUT:

ch = New folder Python 3.8.3 Shell Х File Edit Shell Debug Options Window Help Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020

Hope this helps and clear.

Please feel free to comment if you have any doubts . If you face any difficulty please let me know i will help you with in few minutes.

I strive to provide the best of my knowledge so please upvote if you like the content.

Thank you!

Add a comment
Know the answer?
Add Answer to:
using python The following shows the result of calling function read_from_file_sum_squares() for each of these two...
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
  • Sample program run - Needs to be coded in python -Enter the text that you want to search for, Or DONE when finished: val...

    Sample program run - Needs to be coded in python -Enter the text that you want to search for, Or DONE when finished: valiant paris The valiant Paris seeks for his love. -Enter the text that you want to search for, or DONE when finished: Romeo and Juliet Enter Romeo and Juliet aloft, at the WIndow -- Enter the text that you want to search for, DONE when finished: DONE   # copy the following two lines into any # program...

  • Using the Python program be able to identify or discuss the following: A good test plan...

    Using the Python program be able to identify or discuss the following: A good test plan – how would you determine that it is working? Limitations of the program – what would you do to enhance it? Output improvement – what would you add to improve output? Examples of local variables – name, types, and scope Reason why global variables are not recommended Examples of constants Use of Boolean variable Use of floating point variable Use of integer variable Math...

  • python 3 HW11.2. Read a file and print its lines The function takes a single string...

    python 3 HW11.2. Read a file and print its lines The function takes a single string parameter, which is the name of a file. Complete the function to open the file for reading, read the lines of the file, and print out each line. The function takes a single string parameter, which function to open the file for reading, read the lines student.py def print_lines of file (filename): 1

  • Python Language ! Use the Design Recipe to define a function save_history which consumes two parameters,...

    Python Language ! Use the Design Recipe to define a function save_history which consumes two parameters, a nested list and an int representing the current landing attempt. The function should open a new file named LandingNN.csv' where NN is two digits representing the current landing attempt. The first line should contain the number of sublists in the nested list. Each sublists should be written to the file on its own line with its values separated by commas. This function should...

  • In python Count the frequency of each word in a text file. Let the user choose...

    In python Count the frequency of each word in a text file. Let the user choose a filename to read. 1. The program will count the frequency with which each word appears in the text. 2. Words which are the spelled the same but differ by case will be combined. 3. Punctuation should be removed 4. If the file does not exist, use a ‘try-execption’ block to handle the error 5. Output will list the words alphabetically, with the word...

  • Python 12.10 LAB: Sorting TV Shows (dictionaries and lists) Write a program that first reads in...

    Python 12.10 LAB: Sorting TV Shows (dictionaries and lists) Write a program that first reads in the name of an input file and then reads the input file using the file.readlines() method. The input file contains an unsorted list of number of seasons followed by the corresponding TV show. Your program should put the contents of the input file into a dictionary where the number of seasons are the keys, and a list of TV shows are the values (since...

  • For Python-3 I need help with First creating a text file named "items.txt" that has the...

    For Python-3 I need help with First creating a text file named "items.txt" that has the following data in this order: Potatoes Tomatoes Carrots. Write a python program that 1. Puts this as the first line... import os 2. Creates an empty list 3. Defines main() function that performs the tasks listed below when called. 4. Put these two lines at the top of the main function... if os.path.exists("costlist.txt"): os.remove("costlist.txt") Note: This removes the "costlist.txt" file, if it exists. 5....

  • For this problem, you have to use following hash function: key modulo the number of buckets....

    For this problem, you have to use following hash function: key modulo the number of buckets. Input format: This program takes a file name as argument from the command line. The file is either blank or contains successive lines of input. Each line contains a character, either ‘i’ or ‘s’, followed by a tab and then an integer, the same format as in the Second Part. For each of the lines that starts with ‘i’, your program should insert that...

  • Python 3 Modify the sentence-generator program of Case Study so that it inputs its vocabulary from...

    Python 3 Modify the sentence-generator program of Case Study so that it inputs its vocabulary from a set of text files at startup. The filenames are nouns.txt, verbs. txt, articles.txt, and prepositions.txt. (HINT: Define a single new function, getWords. This function should expect a filename as an argument. The function should open an input file with this name, define a temporary list, read words from the file, and add them to the list. The function should then convert the list...

  • Python 3.6 Question 12 (20 points) Write a function named wordLengths. The function wordLengths takes two...

    Python 3.6 Question 12 (20 points) Write a function named wordLengths. The function wordLengths takes two parameters: 1. inFile, a string that is the name of an input file 2. outFile, a string that is the name of an output file The input file exists when wordLengths is called; wordLengths must create the file outFile. The input file contains only letters and white space For each line of the input file, wordLengths should identify the length of the longest word...

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