Here is my task:
Import the Python csv module.
Create a Python file object in read mode for crime_sampler.csv called csvfile.
Create an empty list called crime_data.
Loop over a csv reader on the file object :
Inside the loop, append the date (first element), type of crime (third element), location description (fifth element), and arrest (sixth element) to the crime_data list.
Remove the first element (headers) from the crime_data list.
Print the first 10 records of the crime_data list. This has been done for you!
# Import the csv module
import csv
# Create the file object: csvfile
csvfile = open('crime_sampler.csv')
# Create an empty list: crime_data
crime_data = []
# Loop over a csv reader on the file object
for row in csvfile:
# Append the date, type of crime, location description, and arrest
crime_data.append((row[0], row[2], row[4], row[5]))
# Remove the first element from crime_data
crime_data.pop(0)
# Print the first 10 records
print(crime_data[:10])
I get unexpected result, i dont know where i'm making a mistake
expected result - [('05/23/2016 05:35:00 PM', 'ASSAULT', 'STREET', 'false'), ('03/26/2016 08:20:00 PM', 'BURGLARY', 'SMALL RETAIL STORE', 'false'), ('04/25/2016 03:05:00 PM', 'THEFT', 'DEPARTMENT STORE', 'true'), ('04/26/2016 05:30:00 PM', 'BATTERY', 'SIDEWALK', 'false'), ('06/19/2016 01:15:00 AM', 'BATTERY', 'SIDEWALK', 'false'), ('05/28/2016 08:00:00 PM', 'BATTERY', 'GAS STATION', 'false'), ('07/03/2016 03:43:00 PM', 'THEFT', 'OTHER', 'false'), ('06/11/2016 06:55:00 PM', 'PUBLIC PEACE VIOLATION', 'STREET', 'true'), ('10/04/2016 10:20:00 AM', 'BATTERY', 'STREET', 'true'), ('02/14/2017 09:00:00 PM', 'CRIMINAL DAMAGE', 'PARK PROPERTY', 'false')]
my result - [('0', '/', '3', '/'), ('0', '/', '6', '/'), ('0', '/', '5', '/'), ('0', '/', '6', '/'), ('0', '/', '9', '/'), ('0', '/', '8', '/'), ('0', '/', '3', '/'), ('0', '/', '1', '/'), ('1', '/', '4', '/'), ('0', '/', '4', '/')]
DATASET
Date,Block,Primary Type,Description,Location Description,Arrest,Domestic,District 05/23/2016 05:35:00 PM,024XX W DIVISION ST,ASSAULT,SIMPLE,STREET,false,true,14 03/26/2016 08:20:00 PM,019XX W HOWARD ST,BURGLARY,FORCIBLE ENTRY,SMALL RETAIL STORE,false,false,24 04/25/2016 03:05:00 PM,001XX W 79TH ST,THEFT,RETAIL THEFT,DEPARTMENT STORE,true,false,6 04/26/2016 05:30:00 PM,010XX N PINE AVE,BATTERY,SIMPLE,SIDEWALK,false,false,15 06/19/2016 01:15:00 AM,027XX W AUGUSTA BLVD,BATTERY,AGGRAVATED: HANDGUN,SIDEWALK,false,false,12 05/28/2016 08:00:00 PM,070XX S ASHLAND AVE,BATTERY,DOMESTIC BATTERY SIMPLE,GAS STATION,false,true,7 07/03/2016 03:43:00 PM,0000X N STATE ST,THEFT,RETAIL THEFT,OTHER,false,false,1 06/11/2016 06:55:00 PM,044XX W MAYPOLE AVE,PUBLIC PEACE VIOLATION,RECKLESS CONDUCT,STREET,true,false,11 10/04/2016 10:20:00 AM,016XX W 63RD ST,BATTERY,SIMPLE,STREET,true,false,7 02/14/2017 09:00:00 PM,018XX S WOOD ST,CRIMINAL DAMAGE,TO CITY OF CHICAGO PROPERTY,PARK PROPERTY,false,false,12