Question

In: Computer Science

In python Complete the function get_Astring(filename) to read the file contents from filename (note that the...

In python Complete the function get_Astring(filename) to read the file contents from filename (note that the test will use the file data.txt and data2.txt provided in the second and third tabs), strip off the newline character at the end of each line and return the contents as a single string.

Solutions

Expert Solution

Program code to copy:-

#This function to reads the contents from file passed as parameter and
#strip off the newline character at the end of each line and
#returns the contents as a single string.
def get_Astring(filename):

    #Opening the file for reading
    fin = open(filename, "r")

    #Declaring a variable to hold the final string after strip off new linw
    string = ""

    #for loop read the content of file line by line and
    #store each line into variable 'line' till end of input file
    for line in fin:
        loc = 0;
        #while loop will check every character of line read from file and
        #add the character to 'string' variable which is not a newline
        while loc < len(line):
            if(line[loc] != '\n'):
                string = string + line[loc]   

            loc = loc + 1

    #closing file
    fin.close()
    #returns string without newline character
    return string    


#Code to test get_Astring() function by calling it in main() function
def main():
    #Prompt and reads filename from user
    filename = input('Enter the file name: ')

    #Calling function to get string without newlinw character
    string = get_Astring(filename)
    #Displaying the value of string returned from function
    print(string)    

#Calling main() function
main()

Screenshot of program code to understand the indentation:-

Screenshot of output:-

Origional content of file(data2.txt) used to test the program:-

Python is a general purpose, interpreted,
platform independent, dynamically typed,
object-oriented programming language.
It has a large collection of in-built library functions.


Related Solutions

In python def lambda_2(filename): # Complete this function to read grades from `filename` and map the...
In python def lambda_2(filename): # Complete this function to read grades from `filename` and map the test average to letter # grades using map and lambda. File has student_name, test1_score, test2_score, # test3_score, test4_score, test5_score. This function must use a lambda # function and map() function. # The input to the map function should be # a list of lines. Ex. ['student1,73,74,75,76,75', ...]. Output is a list of strings in the format # studentname: Letter Grade -- 'student1: C' #...
Python code def plot_dataset(file_path): """ Read in a text file where the path and filename is...
Python code def plot_dataset(file_path): """ Read in a text file where the path and filename is given by the input parameter file_path There are 4 columns in the text dataset that are separated by colons ":". c1:c2:c3:c4 Plot 3 datasets. (x axis vs y axis) c1 vs c2 (Legend label "n=1") c1 vs c3 (Legend label "n=1") c1 vs c4 (Legend label "n=1") Make sure you have proper x and y labels and a title. The x label should be...
In Python write a function with prototype “def wordfreq(filename = "somefile.txt"):” that will read a given...
In Python write a function with prototype “def wordfreq(filename = "somefile.txt"):” that will read a given file that contains words separated by spaces (perhaps multiple words on a line) and will create a dictionary whose keys are the words and the value is the number of times the word appears. Convert each word to lower case before processing.
Use python. redact_file: This function takes a string filename. It writes a new file that has...
Use python. redact_file: This function takes a string filename. It writes a new file that has the same contents as the argument, except that all of the phone numbers are redacted. Assume that the filename has only one period in it. The new filename is the same as the original with '_redacted' added before the period. For instance, if the input filename were 'myfile.txt', the output filename would be 'myfile_redacted.txt'. Make sure you close your output file.
Write a python program that has: function createCustomerRecord that has 1 parameter, the filename to read...
Write a python program that has: function createCustomerRecord that has 1 parameter, the filename to read from, the method then reads all the records from the file and returns a dictionary. Each record in the file has the following format CivilIdNumber Name Telephone#     Address CivilIdNumber Name Telephone#     Address CivilIdNumber Name Telephone#     Address Etc A record always consists of 4 lines (civilid, name, telephone and address). You can find a sample input file on last page of this assignment, copy it...
Ask the user for a filename. In binary format, read all the integers in that file....
Ask the user for a filename. In binary format, read all the integers in that file. Show the number of integers you read in, along with the maximum and minimum integer found. Language is Java. /////////////input///////// n1.dat ////////// output////////// Enter a filename\n Found 50 integers.\n Max: 9944\n Min: 74\n
CODE BLOCK E import csv filename = "D:/python/Week8/files/green.csv" with open(filename) as file: data_from_file = csv.reader(file) header_row...
CODE BLOCK E import csv filename = "D:/python/Week8/files/green.csv" with open(filename) as file: data_from_file = csv.reader(file) header_row = next(data_from_file) for index,column_header in enumerate(header_row): print(index,column_header) How many COUMNS (not rows!) will be printed in the above code?
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT