-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathdata_reader.py
More file actions
32 lines (25 loc) · 894 Bytes
/
data_reader.py
File metadata and controls
32 lines (25 loc) · 894 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import csv
import time
#
# crime14_freq = data_reader.read('crimes_2014.csv', 1, '%d-%b-%y %H:%M:%S', 2014)
# freq = read('311.csv', 0, '%m/%d/%Y', 2014)
def read(filename, date_idx, date_parse, year, bucket=7):
days_in_year = 365
# Create initial frequency map
freq = {}
for period in range(0, int(days_in_year/bucket)):
freq[period] = 0
# Read data and aggregate crimes per day
with open(filename, 'rb') as csvfile:
csvreader = csv.reader(csvfile)
csvreader.next()
for row in csvreader:
if row[date_idx] == '':
continue
t = time.strptime(row[date_idx], date_parse)
if t.tm_year == year and t.tm_yday < (days_in_year-1):
freq[int(t.tm_yday / bucket)] += 1
return freq
if __name__ == '__main__':
freq = read('311.csv', 0, '%m/%d/%Y', 2014)
print freq