In: Computer Science
Python: The attached file (books.csv) contains a list of some of the world's most famous books, including author, title and approximate year written/published. Your task is to write a program to do the following:
Thanks for the question. Below is the code you will be needing, I could not complete everything because I need the csv file to look into the contents are structure. Rest everything is done.
If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.
Thank You!
===========================================================================
import sys
import csv
class Book():
def __init__(self, title, year, author):
self.title = title
self.author = author
try:
self.year = int(year) # convert the year data to an integer,
except:
self.year = 0 # cannot be converted by setting the year for those objects to 0.
def author_first(self):
return f'{self.author}, {self.title}, ({self.year})'
def title_first(self):
return f'{self.title}, by {self.author}'
def main():
filename = sys.argv[1]
#The main function should read and parse the data file into a list of dictionaries
with open(filename,'r') as csv_file:
csv_reader = csv.DictReader(csv_file)
#main function should create an instance of the Book class for
# each line of data, using dictionary
if __name__ == '__main__':
main()
======================================================================
