Question

In: Computer Science

Python: The attached file (books.csv) contains a list of some of the world's most famous books,...

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:

  1. Create a class "Book()" that can store the values passed as arguments at creation time as attributes called "title", "year", and "author". NOTE: Be sure to convert the year data to an integer, and to deal with any data that cannot be converted by setting the year for those objects to 0.
  2. Create two methods on the class, author_first() and title_first() that return, strings in the following forms: "author, title (year)" and "title, by author"
  3. Create a main() function that can receive a command-line argument for the name of the data file using sys.argv.
  4. The main function should read and parse the data file into a list of dictionaries using the csv module (specifically csv.DictReader).
  5. Finally, the main function should create an instance of the Book class for each line of data, using dictionary expansion (that is, using the ** syntax for keyword arguments).

Solutions

Expert Solution

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()

======================================================================


Related Solutions

Python: The file, Program11.txt, on the I: drive contains a chronological list of the World Series’...
Python: The file, Program11.txt, on the I: drive contains a chronological list of the World Series’ winning teams from 1903 through 2018. The first line in the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2018. (Note that the World Series was not played in 1904 or 1994. There are no entries in the file indicating this.) Write a program that reads this file...
In Python. The file “essay.txt” attached to this assignment includes an essay. The essay includes a...
In Python. The file “essay.txt” attached to this assignment includes an essay. The essay includes a couple of sections that are separated by two consecutive newline characters (i.e. ‘\n’) that are shown as empty lines between the sections if you open the file in a text editor like Notepad. Each section starts with a title followed by a couple of paragraphs; the title and the paragraphs are separated by a newline character. Each paragraph includes a couple of sentences that...
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching...
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching pairs of vehicle models and their respective makes Separate out the individual make and model on each line of the file Add the vehicle make to one list, and the vehicle model to another list; such that they are in the same relative position in each list Prompt the user to enter a vehicle model Search the list containing the vehicle models for a...
[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
This is a python file Reads information from a text file into a list of sublists....
This is a python file Reads information from a text file into a list of sublists. Be sure to ask the user to enter the file name and end the program if the file doesn’t exist. Text file format will be as shown, where each item is separated by a comma and a space: ID, firstName, lastName, birthDate, hireDate, salary Store the information into a list of sublists called empRoster. EmpRoster will be a list of sublists, where each sublist...
Using Python. A file exists on the disk named students.txt. The file contains several records, and...
Using Python. A file exists on the disk named students.txt. The file contains several records, and each record contains two fields: (1) the student’s name, and (2) the student’s score for the final exam. Write code that deletes the record containing “John Perz”as the student name. This the code i have so far but it's not working: import os def main(): found=False search='John Perz' student_file = open('student.txt','r') temp_file = open('temp_students.txt','w') name=student_file.readline() score='' while name !='': score=student_file.readline() name=name.rstrip('/n') score=score.rstrip('/n') if name...
. The attached file contains the six variables. I have already attempted this answer and got...
. The attached file contains the six variables. I have already attempted this answer and got it wrong. Please ignore the checkmarks. Question Using the information below select all of the variables that are dichotomous (i.e., two categories). QN88 QN33 _SMOKER3 _SLEPTIM1 QN44 _RFBING5 Behavioral Risk Factor Surveillance System (BRFSS 2016) Calculated Variables https://www.cdc.gov/brfss/annual_data/2016/pdf/2016_calculated_variables_version4.pdf Youth Risk Behavior Surveillance System (YRBSS 2015) YRBS Data User's Guide https://www.cdc.gov/healthyyouth/data/yrbs/pdf/2015/2015_yrbs-data-users_guide_smy_combined.pdf
The United States is home to some of the world's leading computer software companies, most of...
The United States is home to some of the world's leading computer software companies, most of which commonly outsource software development to other countries, including Egypt, India, Ireland, Israel, Malaysia, Hungary, and the Philippines. 1.Why do you think these countries became suppliers to the software industry? 2. Do you think that development of the industry in these countries is a threat to companies in the United States? Explain. Your response to each question should be a minimum of 6 sentences...
What was the Harlem Renaissance, and who were some of the most famous participants?
What was the Harlem Renaissance, and who were some of the most famous participants?
The attached file contains trials data of a school's Track & Field meet's long jump competition....
The attached file contains trials data of a school's Track & Field meet's long jump competition. Find a 95% confidence interval for the difference in the means of populations Grade7 and Grade8. Step 1: Perform an ANOVA and find MSE . Step 2: State the statistic. Step 3: Find t* from the t-distribution. Step 4: Find a 95% confidence interval for the difference in the means of populations Grade7 and Grade8. Step 5: Using Bootstrap, find a 95% confidence interval...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT