Question: Write a function read_tuple that reads text from a file object passed as an argument. The function has to use correctly list comprehension. The function
Write a function read_tuple that reads text from a file object passed as an argument. The function has to use correctly list comprehension.
The function read_tuple takes the following parameters: file_obj: an object representing an open text file; e.g. opened with open(filename,r). types: a tuple of type objects that must match the types of the data entered by the user and returned by the function. The only types supported are int, float, str, bool sep: a string that separates the input objects, with default value ,. The function returns one of: if parsing the data from the user according to the types tuple succeeds, then it returns the tuple with the converted data if parsing the data from the user according to the types tuple fails, then it returns the empty tuple ( ). Error handling: the function must not raise/throw errors related to parsing or input/read operations. Use proper error handling with try/except. In case of any error, print an error message to the terminal and return the empty tuple (). A usage scenario (omitting error handling for brevity) would be: f = open(cars.csv, r) (make, model, mpg_float, modelYr_int, newcar_bool) = read_tuple(f, (str, str, float, int, bool), ,) # use these variables . f.close() This code will work fine if the file cars.csv looks like this: Lada,Niva,19.5,1987,False Porsche,911 Turbo S,17.5,1989,False Function read_tuple() should return in this case the tuple (Lada,Niva,19.5,1987,False) for the first time its called. If the input format does not comply with types argument (str, str, float, int, bool) then the function should return (). c) In the main program (p1.py) write code that tests both functions.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
