Question

In: Computer Science

In the same module/file, write a function processAnimals that takes one argument, the name of an...

In the same module/file, write a function processAnimals that takes one argument, the name of an input file.   The function returns the list of animals created or an empty list if the file didn't contain any lines. The input file contains zero or more lines with the format: species, language, age where species is a string representing an animal's species, language is a string representing an animal's language, and age is an integer representing an animal's age. The items on each line are separated by commas. The processAnimals function should read all lines in the file, creating an Animal object for each line and placing the object into a list. The function also prints to output the result of calling the method speak on each object in the list it created. Don't forget to close the input file. The information below shows how you would call the function processAnimals on an example file. Please note that your implementation should work on any file with the specified format, not just the one provided as a test case:

>>> processAnimals('animals.txt')

I am a 8 year-old cat and I meow.

I am a 22 year-old dog and I bark.

I am a 2 year-old bat and I use sonar.

[Animal('cat','meow',8), Animal('dog','bark',22), Animal('bat','use sonar',2)]

>>> processAnimals('animals.txt')

I am a 8 year-old cat and I meow.

I am a 22 year-old dog and I bark.

I am a 2 year-old bat and I use sonar.

[Animal('cat','meow',8), Animal('dog','bark',22), Animal('bat','use sonar',2)]

>>>

animal test :

cat,meow,8
dog,bark,22
bat,use sonar,2

yse python3.7

Solutions

Expert Solution

Code is as below:

#Import csv will be used to read csv files

import csv

#Animal class definition

class Animal:

    #Constructor of the Animal class

    def __init__(self, species, language, age):

        self.species = species

        self.language = language

        self.age =age

    #__repr__ method is called on each element when you try to print list

    def __repr__(self):

        return "Animal('"+ self.species + "',"+ self.language + "'," + str(self.age) + ")"

    #speak method to display the data as requested

    def speak(self):

        print("I am a " + str(self.age) + " year-old " + self.species + " and I " + self.language + ".")

#processAnimals function to read file

def processAnimals(fileName):

   

    listOfAnimals =[]

    #open the file

    with open(fileName, newline='') as fileToProcess:   

        csvFileData = csv.reader(fileToProcess,delimiter=',')

        for rowData in csvFileData:

            #Create animal object and append to the list

            animal = Animal(rowData[0],rowData[1],rowData[2])

            animal.speak() #call speak method of animal class

            listOfAnimals.append(animal)

    fileToProcess.close() #to close the file

    return listOfAnimals

#Call processAnimals function

AnimalList = processAnimals("animals.txt")

#print the full list

print (AnimalList)

Code screen shot is for your reference and indentation

Output:

When text file is empty:

When text file have data as given:

In case of any query, do comment.

Please rate answer. Thanks


Related Solutions

Write function words() that takes one input argument—a file name—and returns the list of actual words...
Write function words() that takes one input argument—a file name—and returns the list of actual words (without punctuation symbols !,.:;?) in the file. >>> words('example.txt') ['The', '3', 'lines', 'in', 'this', 'file', 'end', 'with', 'the', 'new', 'line', 'character', 'There', 'is', 'a', 'blank', 'line', 'above', 'this', 'line']
Write a bash shell script that takes exactly one argument, a file name. If the number...
Write a bash shell script that takes exactly one argument, a file name. If the number of arguments is more or less than one, print a usage message. If the argument is not a file name, print another message. For the given file, print on the screen its content.
Write a standalone function partyVolume() that takes accepts one argument, a string containing the name of...
Write a standalone function partyVolume() that takes accepts one argument, a string containing the name of a file. The objective of the function is to determine the a Volume object that is the result of many people at a party turning the Volume up and down. More specifically: the first line of the file is a number that indicates the initial value of a Volume object. The remaining lines consist of a single character followed by a space followed by...
Write a parameterized function that takes in a file name as a parameter, reads the file,...
Write a parameterized function that takes in a file name as a parameter, reads the file, calculates the factorial of each number, and displays a formatted output as follows: Factorial of 10 = 3628800 Factorial of 5 = 120
Write a python function image compress() that takes one argument called filename, which is the name...
Write a python function image compress() that takes one argument called filename, which is the name of a file that contains a N × N (N-pixel by N-pixel) “grayscale bitmap image”. A “grayscale bitmap image” is an image of the following form where every pixel contains a grayscale color value between 0 − 255 (inclusive). Colour value 0 means that pixel should appear completely black and color value 255means completely white. Any other value in between stands for different shades...
Write a function in C that takes one argument, an array of 50 elements. Your function...
Write a function in C that takes one argument, an array of 50 elements. Your function should print out the index and value of the smallest element in the array.
In Python, write a function one_bit_NOT that takes one argument, a character that is either '0'...
In Python, write a function one_bit_NOT that takes one argument, a character that is either '0' or '1'. It should perform the NOT operation and return a string with a single character as the result. I.e., if the character argument is "0", it returns a "1"'. If the character argument is "1", it returns a "0".
Write a function redact() takes as input a file name. See secret.txt for the initial test....
Write a function redact() takes as input a file name. See secret.txt for the initial test. The function should print the contents of the file on the screen with this modification: Every occurrence of string 'secret' in the file should be replaced with string 'xxxxxx'. Every occurrence of “agent’ should be replaced with ‘yyyyyy’. Every occurrence of carrier should be replaced with ‘zzzzzz’. This function should catch exceptions. FOR PYTHON
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
Python Programming 1. Write a function name split_name which takes one parameter called name. If the...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the name parameter value is a name in the form LastName, FirstName(e.g., ”Grounds, Nic”) then the function should return a list of two elements where FirstName is the first element and LastName is the second element. If the name parameter value is a name in the form FirstName LastName(e.g., ”Nic Grounds”) then the function should return a list of two elements where FirstName is the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT