In the following code:
JavaScript
x
16
16
1
file = input("Email LIST : ")
2
in_emails = open(file,'r',errors='ignore').read().splitlines()
3
4
domain_set = set()
5
6
out_emails = []
7
8
for email in in_emails:
9
_, tmp = email.split("@", maxsplit=1)
10
domain, _ = tmp.split(":", maxsplit=1)
11
if domain not in domain_set:
12
out_emails.append(email)
13
domain_set.add(domain)
14
15
print(out_emails)
16
I am getting the error below:
JavaScript
1
2
1
_, tmp = email.split("@", maxsplit=1) ValueError: not enough values to unpack (expected 2, got 1)
2
Any solution for ignoring wrong Lines ?
Advertisement
Answer
You can pave over anything that goes wrong and log it like this:
JavaScript
1
11
11
1
for email in in_emails:
2
3
try:
4
_, tmp = email.split("@", maxsplit=1)
5
domain, _ = tmp.split(":", maxsplit=1)
6
if domain not in domain_set:
7
out_emails.append(email)
8
domain_set.add(domain)
9
except Exception as err:
10
print(f"Failed to parse email '{email}'", err)
11