1

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

Mr.Proper
  • 33
  • 5
  • Can you add the specific error you are receiving pasted directly from the error output? It does not look like you are using the csv lib you are importing? – james-see Apr 21 '22 at 20:40
  • my result [('0', '/', '3', '/'), ('0', '/', '6', '/'), ('0', '/', '5', '/'), ('0', '/', '6', '/'), ('0', '/', '9', '/'), ('0', '/', '8', '/'), ('0', '/', '3', '/'), ('0', '/', '1', '/'), ('1', '/', '4', '/'), ('0', '/', '4', '/')] – Mr.Proper Apr 21 '22 at 20:47
  • 1
    edited in question – Mr.Proper Apr 21 '22 at 20:49
  • 1
    @Mr.Proper, don't know what's inside your csv file, but you are definetely not using `csv` module. [Here](https://stackoverflow.com/questions/41585078/how-do-i-read-and-write-csv-files-with-python) you can find how you should do it – sudden_appearance Apr 21 '22 at 20:55
  • How i'm not using it? I imported it as it's said in task. – Mr.Proper Apr 21 '22 at 20:58
  • To clarify the issue, you appear to be accessing 0, 2, 4, and 5 indices of the string values for each `row`, which is why the corresponding digits and `/` from the date at the start of each `row` are printed in your result. – pcamach2 Apr 21 '22 at 21:00

1 Answers1

0

My guess is that your csv data is not formatted properly with quotes around each field, the date field probably is unquoted based on your export. Using the proper csv.reader object will mitigate formatting issues.

I would properly use the csv reader that you import as a lib:

csvfile = csv.reader(open('crime_sampler.csv', 'r'), delimiter=',')
crime_data = []

# Loop over a csv reader on the file object
for row in csvfile:
    print(row)
    # 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])

That should work. Without seeing your source data file I cannot know for sure.

james-see
  • 12,210
  • 6
  • 40
  • 47