-
Notifications
You must be signed in to change notification settings - Fork 1
/
csvparse.py
152 lines (146 loc) · 3.61 KB
/
csvparse.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
import os
import csv
import json
import datetime
start = datetime.datetime.now()
print start
indir = '/home/sushantsusan39/Desktop/'
# Initialize a value dict to be used to store values for each state.
# Initialize a state dict for final values
state_dict = {
'HI': {},
'AK': {},
'FL': {},
'SC': {},
'GA': {},
'AL': {},
'NC': {},
'TN': {},
'RI': {},
'CT': {},
'MA': {},
'ME': {},
'NH': {},
'VT': {},
'NY': {},
'NJ': {},
'PA': {},
'DE': {},
'MD': {},
'WV': {},
'KY': {},
'OH': {},
'MI': {},
'WY': {},
'MT': {},
'ID': {},
'WA': {},
'DC': {},
'TX': {},
'CA': {},
'AZ': {},
'NV': {},
'UT': {},
'CO': {},
'NM': {},
'OR': {},
'ND': {},
'SD': {},
'NE': {},
'IA': {},
'MS': {},
'IN': {},
'IL': {},
'MN': {},
'WI': {},
'MO': {},
'AR': {},
'OK': {},
'KS': {},
'LA': {},
'VA': {}
}
for key in state_dict:
dir_path = indir + key
isFirstrow = True #bool to skip first row read
total_population = 0 #variable declaration for Cognitive
cognitive_yes = 0
cognitive_no = 0
cognitive_na = 0
ambu_no=0
ambu_yes=0
count_1 = 0#variable declaration for vison difficulty
count_2 = 0
hearing_yes = 0 #variable declaration for hearing disability
hearing_no = 0
for file in os.listdir(dir_path):
mycsv = csv.reader(open(dir_path + '/' + file))
for row in mycsv:
if isFirstrow:
isFirstrow = False
continue
#', '.join(row)
#row.split(', ')
#cognitive
drem_value = row[252]
if drem_value == '1':
cognitive_yes += 1
elif drem_value == '2':
cognitive_no += 1
else:
cognitive_na += 1
#ambu
ambu_value = row[249]
if ambu_value =='1': #Yes for Ambu
ambu_yes+=1
elif ambu_value == '2':
ambu_no+=1
#Vison effect Deye
#Deye for vision
deye_count = row[247]
if deye_count == '1':
count_1 += 1
elif deye_count == '2':
count_2 += 1
#Hearing difficulty
#dear
dear_value=row[246]
if dear_value == '1':
hearing_yes += 1
elif dear_value == '2':
hearing_no += 1
#total_population is the same for all difficulties
total_population = cognitive_yes + cognitive_no + cognitive_na
#average cognitive difficulty
avg_cogn_yes = (cognitive_yes * 100.0) / total_population
avg_cogn_no = (cognitive_no * 100.0) / total_population
avg_cogn_na = 100.0 - avg_cogn_no - avg_cogn_yes
#average ambulatory difficulty
avg_ambu_yes = (ambu_yes * 100.0) / total_population
avg_ambu_no = (ambu_no * 100.0) / total_population
avg_ambu_na = 100.0 - avg_ambu_yes - avg_ambu_no
#average vision difficulty
vision_difficulty_yes = (count_1 * 100.0) / total_population
vision_difficulty_no = (count_2 * 100.0)/ total_population
vision_na = 100.0 - vision_difficulty_no - vision_difficulty_yes
#average hearing difficulty
avg_hear_yes = (hearing_yes * 100.0) / total_population
avg_hear_no = (hearing_no * 100.0) / total_population
avg_hear_na = 100.0 - avg_hear_no - avg_hear_yes
state_dict[key]['avg_cogn_yes'] = avg_cogn_yes
state_dict[key]['avg_cogn_no'] = avg_cogn_no
state_dict[key]['avg_cogn_na'] = avg_cogn_na
state_dict[key]['avg_ambu_yes'] = avg_ambu_yes
state_dict[key]['avg_ambu_no'] = avg_ambu_no
state_dict[key]['avg_ambu_na'] = avg_ambu_na
state_dict[key]['vision_difficulty_yes'] = vision_difficulty_yes
state_dict[key]['vision_difficulty_no'] = vision_difficulty_no
state_dict[key]['vision_na'] = vision_na
state_dict[key]['avg_hear_yes'] = avg_hear_yes
state_dict[key]['avg_hear_no'] = avg_hear_no
state_dict[key]['avg_hear_na'] = avg_hear_na
#writing all files to a json file.
with open("data.json","w") as f:
json.dump(state_dict, f)
f.close()
print datetime.datetime.now()