Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a python script called import_marta.py that reads in the given csv file an

ID: 3740352 • Letter: W

Question

Write a python script called import_marta.py that reads in the given csv file and stores the information from the appropriate columns of the csv file into the appropriate table(s) of the database, which you can assume has already been created using your marta-schema.sql script. The name of the csv file and the database will be passed in as command line arguments to your program. Do not hard code the name of the csv file or the database. Your program must be able to read from the csv file whose name is given as the first command line argument and write to the database whose name is given in the second command line argument Your program will be run in the following manner: python import_marta.py passenger data.csv marta localhost root Password for root@localhost: Importing data from passenger_data.csv into marta database on localhost... Done. Don't print the Done. line until the import is complete.

Explanation / Answer

import csv
import MySQLdb

param_1 = sys.argv[1]
param_2 = sys.argv[2]
param_3 = sys.argv[3]  
param_4 = sys.argv[4]

mydb = MySQLdb.connect(host=param_3,
user=param_4,
passwd='',
db=param_2)
cursor = mydb.cursor()

csv_data = csv.reader(file(param_1))

for row in csv_data:
cursor.execute('INSERT INTO details(index, date, route_id, route_name, direction, stop_id, stop_name, ons, offs , vehicle_id )'
'VALUES("%s", "%s", "%s" , "%s", "%s", "%s", "%s", "%s", "%s", "%s")',
row)

mydb.commit()
cursor.close()
print "Done"

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote