-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_emails.py
108 lines (92 loc) · 3.02 KB
/
new_emails.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
import os
import csv
import openpyxl
workspace = "\\\\Lincoln\\Library\\ETDs"
contacts_path = os.path.join(workspace, "MMSIDs", "contacts.csv")
contacts_updated = os.path.join(workspace, "MMSIDs", "contacts_updated.csv")
emails_path = os.path.join(workspace, "ETDWorkspace", "MASTERS_PHD_2008_2022.xlsx")
wb = openpyxl.load_workbook(filename = emails_path)
sheet = wb.active
alumni = []
for alumnus in sheet:
alumni.append([alumnus[1].value, alumnus[2].value, alumnus[3].value, alumnus[4].value, alumnus[11].value])
contacts_out = []
total = 0
matches = 0
with open(contacts_path, newline='', encoding="utf-8") as csvfile:
reader = csv.reader(csvfile)
for row in reader:
total += 1
etd_id = row[1]
name = row[2]
first = row[3]
last = row[4]
year = row[6]
old_email = row[8]
#print (f"looking for {first} {last}")
match_count = 0
for alumnus in alumni:
mailing_name = alumnus[0]
first_name = alumnus[1]
last_name = alumnus[2]
class_year = alumnus[3]
new_email = alumnus[4]
if new_email is None:
new_email = "Empty"
if name.lower() in mailing_name.lower():
match_count += 1
row[10] = new_email
elif last.lower() in last_name.lower() and first.lower() in first_name.lower():
match_count += 1
row[10] = new_email
elif first.lower() in mailing_name.lower() and last.lower() in mailing_name.lower():
match_count += 1
row[10] = new_email
if match_count > 1:
match_count = 0
for alumnus in alumni:
mailing_name = alumnus[0]
first_name = alumnus[1]
last_name = alumnus[2]
class_year = alumnus[3]
new_email = alumnus[4]
if new_email is None:
new_email = "Empty"
if last.lower() in last_name.lower() and first.lower() in first_name.lower():
if year == class_year:
match_count += 1
row[10] = new_email
elif first.lower() in mailing_name.lower() and last.lower() in mailing_name.lower():
if year == class_year:
match_count += 1
row[10] = new_email
if match_count == 0 and " " in first:
match_count = 0
for alumnus in alumni:
mailing_name = alumnus[0]
first_name = alumnus[1]
last_name = alumnus[2]
class_year = alumnus[3]
new_email = alumnus[4]
if new_email is None:
new_email = "Empty"
if last.lower() in last_name.lower() and first.split(" ")[0].lower() in first_name.lower():
if year == class_year:
match_count += 1
row[10] = new_email
elif first.split(" ")[0].lower() + last.lower() in mailing_name.lower():
if year == class_year:
match_count += 1
row[10] = new_email
if match_count == 1:
matches += 1
elif match_count == 0:
print (f"Couldn't find {first} {last}")
else:
print (f"Multiple matches for {first} {last}")
contacts_out.append(row)
print (f"Found {matches} of {total}")
# Write contacts to CSV
with open(contacts_updated, "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerows(contacts_out)