Setup Software Environment


In [1]:
from Cincinnati311CSVDataParser import Cincinnati311CSVDataParser
from csv import DictReader
import os
import re
import urllib2

Download the Cincinnati 311 (Non-Emergency) Service Requests data


In [2]:
data_dir = "./Data"

csv_file_path = os.path.join(data_dir, "cincinnati311.csv")

if not os.path.exists(csv_file_path):

    if not os.path.exists(data_dir):
        os.mkdir(data_dir)
        
    url = 'https://data.cincinnati-oh.gov/api/views' +\
          '/4cjh-bm8b/rows.csv?accessType=DOWNLOAD'

    response = urllib2.urlopen(url)

    html = response.read()

    with open(csv_file_path, 'wb') as h_file:
        h_file.write(html)

Parse the 1st record


In [8]:
h_file = open("./Data/cincinnati311.csv", "r")

fieldnames = [re.sub("_", "", elem.lower())\
              for elem in h_file.readline().rstrip().split(',')]

readerobj = DictReader(h_file, fieldnames)

print readerobj.next()

h_file.close()


{'status': 'CLOS', 'servicenotice': '', 'description': '"Thank you so much for sending a plow so quickly last week when we requested one. Once plowing of main roads is finished, Colter Ave once again desperately needs help! The street is unplowed, but more problematic is the giant snow/ice pile at the entrance to the street from Sutton Avenue. It is almost impassible, and definitely dangerous.Thanks in advance for your help!"', 'statusnotes': '', 'expecteddatetime': '2014-02-06T00:00:00Z', 'lasttableupdate': '03/05/2015 11:07:49 PM +0000', 'requesteddate': '02/05/2014 12:00:00 AM +0000', 'zipcode': '45230', 'longitude': '-84.3911002774317', 'requesteddatetime': '2014-02-05T15:48:00Z', 'addressid': '', 'updateddate': '03/07/2014 12:00:00 AM +0000', 'servicename': '"Slippery streets, request"', 'servicecode': '"SLPYST"', 'address': '"6055 COLTER AV, CINC - GJ2564429820"', 'latitude': '39.0883480578611', 'updateddatetime': '2014-03-07T00:00:00Z', 'agencyresponsible': 'Public Services', 'servicerequestid': 'SR14009309', 'jurisdictionid': 'CINCINNATI'}

Implement a class that parses and cleans a Cincinnati 311 data record


In [5]:
# head -n 3 cincinnati311.csv > sample.csv
h_file = open("./Data/sample.csv", "r")

parserobj = Cincinnati311CSVDataParser(h_file)

for record in parserobj:
    print record

h_file.close()


None
{'status': 'clos', 'servicenotice': '', 'description': 'thank you so much for sending a plow so quickly last week when we requested one. once plowing of main roads is finished, colter ave once again desperately needs help! the street is unplowed, but more problematic is the giant snow/ice pile at the entrance to the street from sutton avenue. it is almost impassible, and definitely dangerous.thanks in advance for your help!', 'statusnotes': '', 'expecteddatetime': datetime.datetime(2014, 2, 6, 0, 0, tzinfo=tzutc()), 'lasttableupdate': datetime.datetime(2015, 3, 5, 23, 7, 49, tzinfo=tzutc()), 'requesteddate': datetime.datetime(2014, 2, 5, 0, 0, tzinfo=tzutc()), 'zipcode': '45230', 'longitude': '-84.3911002774317', 'requesteddatetime': datetime.datetime(2014, 2, 5, 15, 48, tzinfo=tzutc()), 'addressid': '', 'updateddate': datetime.datetime(2014, 3, 7, 0, 0, tzinfo=tzutc()), 'servicename': 'slippery streets, request', 'servicecode': 'slpyst', 'address': '6055 colter av, cinc - gj2564429820', 'latitude': '39.0883480578611', 'updateddatetime': datetime.datetime(2014, 3, 7, 0, 0, tzinfo=tzutc()), 'agencyresponsible': 'public services', 'servicerequestid': 'sr14009309', 'jurisdictionid': 'cincinnati'}
{'status': 'clos', 'servicenotice': '', 'description': "ehrman ave. is very slippery/traffic can't make the hill/street often forgotten.", 'statusnotes': '', 'expecteddatetime': datetime.datetime(2014, 2, 6, 0, 0, tzinfo=tzutc()), 'lasttableupdate': datetime.datetime(2015, 3, 5, 23, 7, 49, tzinfo=tzutc()), 'requesteddate': datetime.datetime(2014, 2, 5, 0, 0, tzinfo=tzutc()), 'zipcode': '45220', 'longitude': '-84.5091154538766', 'requesteddatetime': datetime.datetime(2014, 2, 5, 15, 48, tzinfo=tzutc()), 'addressid': '', 'updateddate': datetime.datetime(2014, 3, 7, 0, 0, tzinfo=tzutc()), 'servicename': 'slippery streets, request', 'servicecode': 'slpyst', 'address': 'ehrman av & vine st - gj1524336568 ', 'latitude': '39.1517133255434', 'updateddatetime': datetime.datetime(2014, 3, 7, 0, 0, tzinfo=tzutc()), 'agencyresponsible': 'public services', 'servicerequestid': 'sr14009310', 'jurisdictionid': 'cincinnati'}

In [ ]: