-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_many_files.py
335 lines (301 loc) · 15.1 KB
/
read_many_files.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 11 20:10:53 2014
@author: Eric
Reads in many csv and fixed-width files at once.
Sources:
- 2008_to_2012_age_and_sex
- Searched the US Census' FactFinder with Geographies: {'County': ['All Counties within United States', 'All Counties within United States and Puerto Rico']}
- Data from S0101: AGE AND SEX
- 2012 ACS 5-year estimates
- Downloaded 2014-05-04
- 2008_to_2012_race_and_ethnicity
- Searched the US Census' FactFinder with Geographies: {'County': ['All Counties within United States', 'All Counties within United States and Puerto Rico']}
- Data from B03002: HISPANIC OR LATINO ORIGIN BY RACE
- 2012 ACS 5-year estimates
- Downloaded 2014-05-04
- 2008_to_2012_social_characteristics
- Searched the US Census' FactFinder with Geographies: {'County': ['All Counties within United States', 'All Counties within United States and Puerto Rico']}
- Data from DP02: SELECTED SOCIAL CHARACTERISTICS IN THE UNITED STATES
- 2012 ACS 5-year estimates
- Downloaded 2014-05-04
- 2010_to_2013_population
- County-level population estimates
- Data downloaded from http://www.census.gov/popest/data/counties/totals/2013/CO-EST2013-01.html, 2014-05-04
- 2012_income_and_poverty
- County-level estimates of US poverty and income for 2012
- Direct link: http://www.census.gov/did/www/saipe/downloads/estmod12/est12ALL.txt
- Guide to fields can be found http://www.census.gov/did/www/saipe/data/statecounty/data/2012.html
- 2013_area
- From http://www.census.gov/geo/maps-data/data/gazetteer2013.html; see link for column info
- Downloaded 2014-05-04
- 2014_health_indicators
- Available from http://www.countyhealthrankings.org/rankings/data, a program from Robert Wood Johnson
- The data in the last 10 counties in Wyoming were corrupted, so I had to delete them manually.
- unemployment_statistics
- From http://www.bls.gov/lau/tables.htm
- laucntycur14.txt downloaded 2014-09-13, laucntycur13.txt downloaded 2014-09-22, others downloaded 2014-02
Suffixes at the end of variable names:
a: numpy array
b: boolean
d: dictionary
df: pandas DataFrame
l: list
s: string
t: tuple
Underscores indicate chaining: for instance, "foo_t_t" is a tuple of tuples
"""
import os
import config
reload(config)
import utilities
reload(utilities)
def main(con, cur):
""" Read and transform many data sources needed for the county_data_analysis database. """
# Fields to extract: first level indicates folder, second level indicates
# file name, third level indicates fields. In length-2 tuples, index 0
# represents the field start column for fixed-width files and index 1
# represents the length of the field.
field_d = {'2008_to_2012_age_and_sex':
{'ACS_12_5YR_S0101_with_ann.csv':
{'delimiter': ',',
'lines_to_ignore': 2,
'new_line_string': '\r\n',
'null_string': '*****',
'total_num_fields': 219,
'fields': {2: 'fips_column',
179: 'median_age',
185: 'sex_ratio'}}},
'2008_to_2012_race_and_ethnicity':
{'ACS_12_5YR_B03002_with_ann.csv':
{'delimiter': ',',
'lines_to_ignore': 2,
'new_line_string': '\r\n',
'null_string': '*****',
'total_num_fields': 46,
'fields': {2: 'fips_column',
5: '2008_to_2012_race_and_ethnicity_total',
9: 'white_not_hispanic_number',
11: 'black_not_hispanic_number',
15: 'asian_not_hispanic_number',
27: 'hispanic_number'}}},
'2008_to_2012_social_characteristics':
{'ACS_12_5YR_DP02_with_ann.csv':
{'delimiter': ',',
'lines_to_ignore': 2,
'new_line_string': '\r\n',
'null_string': '(X)',
'total_num_fields': 600,
'fields': {2: 'fips_column',
55: 'percent_households_with_children',
59: 'percent_households_with_senior_citizens',
61: 'average_household_size',
103: 'percent_never_married',
119: 'percent_divorced',
157: 'fertility_rate',
267: 'percent_high_school_graduate',
279: 'percent_veterans',
319: 'percent_same_house_1_yr_ago',
371: 'percent_foreign_born',
451: 'percent_english_not_spoken_at_home'}}},
'2010_to_2013_population':
{'PEP_2013_PEPANNRES_with_ann.csv':
{'delimiter': ',',
'lines_to_ignore': 2,
'new_line_string': '\r\n',
'null_string': None,
'total_num_fields': 10,
'fields': {2: 'fips_column',
5: 'population_2010_census',
10: 'population_2013_estimate'}}},
'2012_income_and_poverty':
{'est12ALL.txt':
{'delimiter': None,
'lines_to_ignore': 0,
'new_line_string': '\r\n',
'null_string': None,
'fields': {(1, 2): 'fips_state_column',
(4, 3): 'fips_county_column',
(35, 4): 'percent_in_poverty',
(134, 6): 'median_household_income'}}},
'2013_area':
{'2013_Gaz_counties_national.txt':
{'delimiter': r'\t',
'lines_to_ignore': 1,
'new_line_string': '\r\n',
'null_string': None,
'total_num_fields': 10,
'fields': {2: 'fips_column',
7: 'land_area'}}},
'2014_health_indicators':
{'2014_CHR_analytic_data.csv':
{'delimiter': ',',
'lines_to_ignore': 2,
'new_line_string': '\r\n',
'null_string': '',
'total_num_fields': 324,
'fields': {1: 'fips_state_column',
2: 'fips_county_column',
6: 'premature_death_rate',
31: 'fraction_smoking',
36: 'fraction_obese',
71: 'teen_birth_rate',
76: 'fraction_non_senior_citizens_without_insurance'}}},
'unemployment_statistics':
{'laucnty08.txt':
{'delimiter': None,
'lines_to_ignore': 6,
'new_line_string': '\r\n',
'null_string': None,
'fields': {(19, 2): 'fips_state_column',
(26, 3): 'fips_county_column',
(129, 4): 'unemployment_rate_2008'}},
'laucnty12.txt':
{'delimiter': None,
'lines_to_ignore': 6,
'new_line_string': '\r\n',
'null_string': None,
'fields': {(19, 2): 'fips_state_column',
(26, 3): 'fips_county_column',
(129, 4): 'unemployment_rate_2012'}},
'laucnty13.txt':
{'delimiter': None,
'lines_to_ignore': 6,
'new_line_string': '\r\n',
'null_string': None,
'fields': {(19, 2): 'fips_state_column',
(26, 3): 'fips_county_column',
(129, 4): 'unemployment_rate_2013'}}}}
## Read in files
for folder_name in field_d:
for file_name in field_d[folder_name]:
# Prepare for creating table
table_name = file_name.replace('.', '')
print('Loading {table_name}.'.format(table_name=table_name))
file_path = os.path.join(config.raw_data_path_s, folder_name,
file_name)
cur.execute('DROP TABLE IF EXISTS {table_name};'.format(table_name=
table_name))
# Create table
command_s = 'CREATE TABLE {table_name}('
command_s = command_s.format(table_name=table_name)
this_table_field_d = field_d[folder_name][file_name]['fields']
for field in this_table_field_d:
field_s = '{field_name} FLOAT(16, 5), '
field_s = field_s.format(field_name=this_table_field_d[field])
command_s += field_s
command_s = command_s[:-2] + ');'
cur.execute(command_s)
## Load all columns
if field_d[folder_name][file_name]['delimiter']:
# CSV or tab-delimited tables
# Start command
command_s = """LOAD DATA LOCAL INFILE '{file_path}'
INTO TABLE {table_name}"""
command_s = command_s.format(file_path=file_path, table_name=table_name)
command_s = command_s.replace('\\', r'\\')
command_s += r"""
FIELDS TERMINATED BY '{delimiter}'
LINES TERMINATED BY '{new_line_string}'
IGNORE {lines_to_ignore} LINES"""
command_s = command_s.format( \
delimiter=field_d[folder_name][file_name]['delimiter'],
lines_to_ignore=field_d[folder_name][file_name]['lines_to_ignore'],
new_line_string=field_d[folder_name][file_name]['new_line_string'])
# Add list of fields
total_num_fields = field_d[folder_name][file_name]['total_num_fields']
command_s += utilities.construct_field_string(total_num_fields)
# Add a list of field name correspondences: set as NULL if necessary
command_s += """
SET """
if field_d[folder_name][file_name]['null_string'] != None:
null_string = field_d[folder_name][file_name]['null_string']
for field_num, field_name in \
field_d[folder_name][file_name]['fields'].iteritems():
command_s += """%s = NULLIF(@col%03d, '%s'), """ % \
(field_name, field_num, null_string)
else:
for field_num, field_name in \
field_d[folder_name][file_name]['fields'].iteritems():
command_s += '%s = @col%03d, ' % (field_name, field_num)
command_s = command_s[:-2] + ';'
else:
# Fixed-width tables
# Start command
command_s = """LOAD DATA LOCAL INFILE '{file_path}'
INTO TABLE {table_name}"""
command_s = command_s.format(file_path=file_path, table_name=table_name)
command_s = command_s.replace('\\', r'\\')
command_s += r"""
LINES TERMINATED BY '\r\n'
IGNORE {lines_to_ignore} LINES
(@whole_row)"""
command_s = command_s.format( \
lines_to_ignore=field_d[folder_name][file_name]['lines_to_ignore'])
# Tell MySQL to add column ranges adding to the fields we want
# (inspiration: http://stackoverflow.com/questions/11461790/loa
# ding-fixed-width-space-delimited-txt-file-into-mysql)
command_s += """
SET """
for field_num, field_name in \
field_d[folder_name][file_name]['fields'].iteritems():
command_s += '%s = TRIM(SUBSTR(@whole_row, %d, %d)), ' \
% (field_name, field_num[0], field_num[1])
command_s = command_s[:-2] + ';'
# print(command_s)
cur.execute(command_s)
## Create proper FIPS column
if 'fips_column' in field_d[folder_name][file_name]['fields'].values():
command_s = """ALTER TABLE {table_name}
CHANGE fips_column fips_column INT;""".format(table_name=table_name)
cur.execute(command_s)
command_s = """ALTER TABLE {table_name}
CHANGE fips_column {table_name}_fips CHAR(5);""".format(table_name=table_name)
cur.execute(command_s)
else:
# Cast FIPS columns as CHAR
command_s = """ALTER TABLE {table_name}
CHANGE fips_state_column fips_state_column INT,
CHANGE fips_county_column fips_county_column INT;""".format(table_name=table_name)
cur.execute(command_s)
command_s = """ALTER TABLE {table_name}
CHANGE fips_state_column fips_state_column CHAR(2),
CHANGE fips_county_column fips_county_column CHAR(3);"""
command_s = command_s.format(table_name=table_name)
cur.execute(command_s)
# Print columns
# print('First rows of {table_name}, before padding:'.format(table_name=table_name))
# cur.execute('SELECT * FROM {table_name};'.format(table_name=table_name))
# for l_row in range(10):
# row = cur.fetchone()
# print(row)
# Pad out fips_county_column
command_s = """UPDATE {table_name}
SET fips_county_column = LPAD(fips_county_column, 3, '0');""".format(table_name=table_name)
cur.execute(command_s)
# Print columns
# print('First rows of {table_name}, after padding:'.format(table_name=table_name))
# cur.execute('SELECT * FROM {table_name};'.format(table_name=table_name))
# for l_row in range(10):
# row = cur.fetchone()
# print(row)
# Concatenate the two FIPS fields
command_s = 'ALTER TABLE {table_name} ADD {table_name}_fips VARCHAR(5);'
command_s = command_s.format(table_name=table_name)
cur.execute(command_s)
command_s = """UPDATE {table_name}
SET {table_name}_fips = CONCAT(fips_state_column, fips_county_column);"""
command_s = command_s.format(table_name=table_name)
cur.execute(command_s)
# Delete unused columns
command_s = """ALTER TABLE {table_name}
DROP fips_state_column,
DROP fips_county_column;""".format(table_name=table_name)
cur.execute(command_s)
# Print columns
# print('First rows of {table_name}:'.format(table_name=table_name))
# cur.execute('SELECT * FROM {table_name};'.format(table_name=table_name))
# for l_row in range(10):
# row = cur.fetchone()
# print(row)
return field_d