-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.py
More file actions
222 lines (208 loc) · 5.8 KB
/
parser.py
File metadata and controls
222 lines (208 loc) · 5.8 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import os
import csv
import sys
from tqdm import tqdm
ROOT="./"
FARS_TEMPLATE="FARS_NationalCSV"
CONFIG_PATH=os.path.join(ROOT, 'config')
def grabFiles(farsFolder):
'''
Function grabs all filenames within
a given FARS folder
'''
for _,_,f in os.walk(farsFolder):
pass
return f
def grabCat(farsFileList):
'''
Function returns list of files without file extension.
Used to categorize data from parser function
'''
return [fars[:-4] for fars in farsFileList]
def generateFARS(year):
'''
Function returns properly formatted FARS report
folder name using parser.FARS_TEMPLATE
'''
return FARS_TEMPLATE.replace('_', str(year))
def csvRead(filename, filetype):
'''
Function reads csv file and returns list
of OrderDicts
'''
with open(filename, 'r') as csvFile:
csvReader = csv.DictReader(csvFile)
_ret = list(csvReader)
_ret = [itemParser10(ret, filetype) for ret in _ret]
return _ret
def loadConfig(year):
'''
Function should read and return a configuration file for
a given FARS year (FARS number and file naming convention
can change.
NOTE: Since FARS 2010 Standardization Act, all following FARS
reports should be the same format...
'''
with open(os.path.join(CONFIG_PATH, "%.txt"%(FARS_TEMPLATE.replace('_', year)))) as configFile:
_config = config.readlines()
return _config
def itemParser10(item, filetype):
'''
Function fixes datatype mismatch within any FARS report 2010 or later
(standardization act compliant)
'''
if filetype == 'NMIMPAIR':
for key in item.keys():
if item[keu] == '':
pass
else:
item[key] = int(item[key])
elif filetype == 'DRIMPAIR':
for key in items.keys():
if item[key] == '':
pass
else:
item[key] = int(item[key])
elif filetype == 'SAFETYEQ':
for key in items.keys():
if item[key] == '':
pass
else:
item[key] = int(item[key])
elif filetype == 'ACCIDENT':
for key in item.keys():
if item[key] == '':
pass
if key == 'LATITUDE' or key == 'LONGITUD':
item[key] = float(item[key])
elif key == 'TWAY_ID' or key == 'TWAY_ID2' or key == 'RAIL':
pass
else:
item[key] = int(item[key])
elif filetype == 'VIOLATN':
for key in item.keys():
if item[key] == '':
pass
else:
item[key] = int(item[key])
elif filetype == 'CEVENT':
for key in item.keys():
if item[key] == '':
pass
else:
item[key] = int(item[key])
elif filetype == 'VINDECODE':
pass
elif filetype == 'MIDRVACC':
pass
elif filetype == 'NMCRASH':
pass
elif filetype == 'VSOE':
pass
elif filetype == 'DAMAGE':
pass
elif filetype == 'MIACC':
pass
elif filetype == 'ACC_AUX':
pass
elif filetype == 'PER_AUX':
pass
elif filetype == 'VEVENT':
pass
elif filetype == 'FACTOR':
pass
elif filetype == 'MANEUVER':
pass
elif filetype == 'PERSON':
pass
elif filetype == 'PBTYPE':
pass
elif filetype == 'MIPER':
pass
elif filetype == 'DRUGS':
pass
elif filetype == 'VEH_AUX':
pass
elif filetype == 'PARKWORK':
pass
elif filetype == 'VEHICLE':
pass
elif filetype == 'VISION':
pass
elif filetype == 'DISTRACT':
pass
elif filetype == 'NMPRIOR':
pass
else:
print("WARNING: No case detected! File may not follow 2010+ format.")
return item
def readSpeed(filename):
with open(filename) as csvFile:
csvReader = csv.DictReader(csvFile)
_ret = list(csvReader)
__ret = [(i['State'],int(i['Speed Limit'])) for i in _ret]
return __ret
def mapState(input_state):
orderedStates = [
'Alabama',
'Alaska',
'',
'Arizona',
'Arkansas',
'California',
'',
'Colorado',
'Connecticut',
'Delaware',
'District of Columbia',
'Florida',
'Georgia',
'',
'Hawaii',
'Idaho',
'Illinois',
'Indiana',
'Iowa',
'Kansas',
'Kentucky',
'Louisiana',
'Maine',
'Maryland',
'Massachusetts',
'Michigan',
'Minnesota',
'Mississippi',
'Missouri',
'Montana',
'Nebraska',
'Nevada',
'New Hampshire',
'New Jersey',
'New Mexico',
'New York',
'North Carolina',
''
]
if input_state <= 2:
return orderedState[input_state+1]
elif input_state <= 6:
return orderedState[input_state-1]
elif input_state <= 13:
return orderedStates[input_state-2]
return 1
if __name__ == '__main__':
readSpeed('highway_speed.csv')
'''
# API Testing area:
fars = generateFARS(2018)
fars_2018_files = grabFiles(fars)
#for far in fars_2018_files:
# print(far)
index = 3
print(fars_2018_files[index])
category = grabCat(fars_2018_files)
_filecat = category[index]
test = csvRead(os.path.join(ROOT, fars, fars_2018_files[index]), _filecat)
print(test[0])
print(len(test[0]))
'''