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

Please check and correct my code. Question B2: CSV file [2 points) Write Python

ID: 3897046 • Letter: P

Question

Please check and correct my code.

Question B2: CSV file [2 points) Write Python code that reads the file drug_arrests.csv and produces two lists: • dates should be a list of integers from the year column in the file (as integers). • arrests should be a list of arrests for marijuana posession from the arrests column in the file, as integers In [59]: # YOUR CODE HERE import csv with open('drug_arrests.csv') as csvfile: readCSV = csv.reader(csvfile, delimiter=',') dates = [] arrests = [] for row in readCSV: d=row[1] dates.append(d) if 'marijuana' in row[2): a=row[0] arrests.append(a) print(dates) print(arrests) IndexError Traceback (most recent call last) in

Explanation / Answer

Here, assumming the following is the given sample format for 'drug_arrests.csv' file

1234,23,qwe
1235,123,tre
1236,12,twi
1237,87,marijuana
1238,90,marijuana
1239,12,wer

Therefore, Code should as following with a little change

import csv
with open('drug_arrests.csv') as csvfile:
readCSV = csv.reader(csvfile,delimiter=',')
dates=[]
arrests = []
for row in readCSV:
d = int(row[1])
dates.append(d)
if 'marijuana' in row[2]:
a=int(row[0])
arrests.append(a)
print(dates)
print(arrests)