-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcombine.py
302 lines (241 loc) · 8.06 KB
/
combine.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
'''Python program to combine CVS mailing lists into single master one'''
import os
import csv
def check_exists(name_in, dic):
'''Checks if company name already in dic.'''
if name_in in dic:
print(name_in, ' Exists!!!')
return True
return False
mast_dict = {}
repeat_dict = {}
pathName = os.getcwd()
pathName += os.path.join(pathName, '/mailing-lists/CSV/')
print('pathName: ', pathName)
print('Start combining')
print('*******************')
for filename in os.listdir(pathName):
print(filename)
# Open file and read it in
file = open(os.path.join(pathName, filename), "r")
reader = csv.reader(file, delimiter=',')
if filename == 'AC-MI-Mailing-List.csv':
print('u found it')
firstline = True
for row in reader:
if firstline:
firstline = False
continue
# Check if company name already exists
c_name = row[3]
if not check_exists(c_name, mast_dict):
mast_dict[c_name] = {}
# TODO: Makes names not uppercase
# fullname = row[0] + '' + row[1]
# mast_dict[c_name]['contact'] = fullname
mast_dict[c_name]['firstname'] = row[0].title()
mast_dict[c_name]['lastname'] = row[1].title()
mast_dict[c_name]['title'] = row[2]
mast_dict[c_name]['address'] = row[4]
mast_dict[c_name]['city'] = row[5]
# Check for Michigan vs MI, etc
if row[7] == 'Michigan':
mast_dict[c_name]['state'] = 'MI'
elif row[7] == 'Ohio':
mast_dict[c_name]['state'] = 'OH'
else:
mast_dict[c_name]['state'] = row[7]
mast_dict[c_name]['zip'] = row[8]
mast_dict[c_name]['phone'] = row[9]
mast_dict[c_name]['url'] = row[10]
mast_dict[c_name]['description'] = row[11]
mast_dict[c_name]['employees'] = row[12]
mast_dict[c_name]['is_Compressor'] = True
else:
# key = phone, val = []
if c_name not in repeat_dict:
repeat_dict[c_name] = []
repeat_dict[c_name].append(row)
elif filename == 'Kaeser-Jan18toApril19.csv':
firstline = True
for row in reader:
if firstline:
firstline = False
continue
# Check if company name already exists
# Note: Need to remove extra space after company name
c_name = row[0]
c_name = c_name[:-1]
if not check_exists(c_name, mast_dict):
mast_dict[c_name] = {}
# Split into first/last names
hold = row[7].split()
# print('hold:', hold)
mast_dict[c_name]['firstname'] = hold[0].title()
mast_dict[c_name]['lastname'] = hold[1].title()
mast_dict[c_name]['title'] = row[11]
# mast_dict[c_name]['address'] = row[4]
mast_dict[c_name]['city'] = row[3]
# Check for Michigan vs MI, etc
if row[4] == 'Michigan':
mast_dict[c_name]['state'] = 'MI'
elif row[4] == 'Ohio':
mast_dict[c_name]['state'] = 'OH'
else:
mast_dict[c_name]['state'] = row[4]
mast_dict[c_name]['zip'] = row[5]
mast_dict[c_name]['phone'] = row[8]
mast_dict[c_name]['email'] = row[9]
# mast_dict[c_name]['url'] = row[10]
mast_dict[c_name]['description'] = row[6]
# mast_dict[c_name]['employees'] = row[12]
mast_dict[c_name]['is_Compressor'] = True
else:
if c_name not in repeat_dict:
repeat_dict[c_name] = []
repeat_dict[c_name].append(row)
elif filename == 'Compressor Email List.csv':
firstline = True
for row in reader:
if firstline:
firstline = False
continue
c_name = row[3]
if c_name != None and row[1] != '':
if not check_exists(c_name, mast_dict):
mast_dict[c_name] = {}
# Split into first/last names
hold = row[1].split()
# print('hold:', hold)
mast_dict[c_name]['firstname'] = hold[0].title()
if len(hold) > 1:
mast_dict[c_name]['lastname'] = hold[1].title()
mast_dict[c_name]['email'] = row[0]
mast_dict[c_name]['is_Compressor'] = True
else:
if c_name not in repeat_dict:
repeat_dict[c_name] = []
repeat_dict[c_name].append(row)
elif filename == 'Air Center Tools Email List.csv':
firstline = True
for row in reader:
if firstline:
firstline = False
continue
c_name = row[2]
if not check_exists(c_name, mast_dict):
mast_dict[c_name] = {}
mast_dict[c_name]['firstname'] = row[1].title()
mast_dict[c_name]['lastname'] = row[0].title()
mast_dict[c_name]['phone'] = row[4]
mast_dict[c_name]['email'] = row[3]
mast_dict[c_name]['is_Compressor'] = False
else:
# key = phone, val = []
if c_name not in repeat_dict:
repeat_dict[c_name] = []
row.append('TOOL')
repeat_dict[c_name].append(row)
elif filename == 'export from compressor.csv':
firstline = True
for row in reader:
if firstline:
firstline = False
continue
c_name = row[9]
if c_name != '' and row[2] != '':
if not check_exists(c_name, mast_dict):
mast_dict[c_name] = {}
# Get names
if row[3] == '':
hold = row[2].split()
if len(hold) > 1:
mast_dict[c_name]['firstname'] = hold[0].title()
mast_dict[c_name]['lastname'] = hold[-1].title()
else:
mast_dict[c_name]['firstname'] = row[2].title()
else:
mast_dict[c_name]['firstname'] = row[2].title()
mast_dict[c_name]['lastname'] = row[3].title()
mast_dict[c_name]['phone'] = row[4]
mast_dict[c_name]['email'] = row[1]
if row[27] == 'Michigan':
mast_dict[c_name]['state'] = 'MI'
if row[12] == 'Air Compressors':
mast_dict[c_name]['is_Compressor'] = True
elif row[12] == 'Assembly Tool':
mast_dict[c_name]['is_Compressor'] = False
else:
firstline = True
for row in reader:
if firstline:
firstline = False
continue
# Check if company name already exists
c_name = row[0]
if not check_exists(c_name, mast_dict):
mast_dict[c_name] = {}
mast_dict[c_name]['address'] = row[1]
mast_dict[c_name]['city'] = row[2]
# Check for Michigan vs MI, etc
if row[3] == 'Michigan':
mast_dict[c_name]['state'] = 'MI'
elif row[3] == 'Ohio':
mast_dict[c_name]['state'] = 'OH'
else:
mast_dict[c_name]['state'] = row[3]
mast_dict[c_name]['zip'] = row[4]
mast_dict[c_name]['phone'] = row[5]
# mast_dict[c_name]['email'] = row[9]
mast_dict[c_name]['url'] = row[6]
mast_dict[c_name]['description'] = row[11]
mast_dict[c_name]['employees'] = row[8]
mast_dict[c_name]['parent'] = row[10]
mast_dict[c_name]['is_Compressor'] = True
else:
if c_name not in repeat_dict:
repeat_dict[c_name] = []
repeat_dict[c_name].append(row)
# Write mast_dict to CSV file
with open('master.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, quoting=csv.QUOTE_ALL)
top = ['Company Name', 'Is Compressor', 'First Name', 'Last Name', 'Title',
'Address', 'City', 'Zip Code', 'State',
'Phone', 'Email', 'URL', 'Description', 'Employees', 'Parent Company']
writer.writerow(top)
for key,val in mast_dict.items():
out_list = []
# print('VAL:', val)
out_list.append(key)
out_list.append(val.get('is_Compressor', None))
out_list.append(val.get('firstname', None))
out_list.append(val.get('lastname', None))
out_list.append(val.get('title',None))
out_list.append(val.get('address',None))
out_list.append(val.get('city',None))
out_list.append(val.get('zip',None))
out_list.append(val.get('state', None))
out_list.append(val.get('phone', None))
out_list.append(val.get('email', None))
out_list.append(val.get('url', None))
out_list.append(val.get('description', None))
out_list.append(val.get('employees', None))
out_list.append(val.get('parent', None))
writer.writerow(out_list)
# Write Repeats to CSV file
with open('repeats.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, quoting=csv.QUOTE_ALL)
# top = ['Company Name', 'First Name', 'Last Name', 'Title',
# 'Address', 'City', 'Zip Code', 'State',
# 'Phone', 'URL', 'Description', 'Employees', 'Parent Company']
# writer.writerow(top)
for key, val in repeat_dict.items():
# print(key, val)
writer.writerow([key])
for entry in val:
writer.writerow(entry)
print('Finished, length of master = ', len(mast_dict))
# print(mast_dict['826 Michigan'])
# print(mast_dict['Aria Energy'])
# print(mast_dict['General Motors Company'])
# print(repeat_dict)