-
Notifications
You must be signed in to change notification settings - Fork 0
/
311DateZip.py
88 lines (61 loc) · 3.2 KB
/
311DateZip.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import gzip
import sys
#input complaints.txt --> all complaints that interest the hypothesis
#output a set that contains all those complaints (so look up is only O(1))
def getComplaints(fin):
# create set of complaints of interest to our hypothesis
complaints = set()
# open list of complaints of interest
f = open(fin)
# read in from file every complaint
line = f.readline().strip("\n").upper()
while line:
# add the complaint
complaints.add(line)
# move to the next line
line = f.readline().strip("\n").upper()
return complaints
#input 311_Service_Requests_from_2010_to_Present.tsv.gz and set containing complaints of interest
#output txt file with the date and the zip code of the complaint
# date|zip
def dateLatLong(finName, fout, complaintsOfInterest):
#open files
#input
f = gzip.open(finName, "rt")
#output
fout = open(fout, "w")
#read starting from the second line
line = f.readline()
line = f.readline()
# keep track of "bad records" that we are not considering
badRecords = 0
# keep track of "uninteresting records"
uninterestingRecords = 0
while line:
try:
#split it on tabs and strip spaces
fields = line.split("\t")
# if the complaint (field Complaint Type) is part of the list of complaints that are relevant to the hypothesis
# AND
# if all fields are valid (non empty)
if fields[1] != "" and fields[8] != "" and fields[5].upper() in complaintsOfInterest:
fout.write(fields[1][0:10] + "|" + fields[8] + "\n")
# otherwise it's an "uninterestingLine"
else: uninterestingLines += 1
except:
badRecords += 1
line = f.readline()
# total discarded records
totDiscarded = badRecords + uninterestingRecords
#close files
f.close()
fout.close()
def main():
#only get complaints relevant to our hypothesis
complaints = getComplaints("complaints.txt")
# read in complaints data
finName311 = "311_Service_Requests_from_2010_to_Present.tsv.gz"
# output date and zip
fout311 = "311DateZip.txt"
keys311 = dateLatLong(finName311, fout311, complaints)
main()