-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_emails.py
46 lines (38 loc) · 1.16 KB
/
extract_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
#Coded by: QyFashae
import re
print("extract emails from a txt file with the help of 'Regex' for emails")
fileToRead = input("file to read: ") # File to extract from
fileToWrite = input("file to write: ") # File to write to
delimiterInFile = [
",",
";",
] # Change here whether it is for example [email protected];[email protected] or ';'
def validateEmail(strEmail):
if re.match(
"[a-zA-Z0-9.!#$%&'+-/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)", strEmail
):
return True
return False
def writeFile(listData):
file = open(fileToWrite, "w+")
strData = ""
for item in listData:
strData = strData + item + "\n"
file.write(strData)
listEmail = []
file = open(fileToRead, "r")
listLine = file.readlines()
for itemLine in listLine:
item = str(itemLine)
for delimeter in delimiterInFile:
item = item.replace(str(delimeter), " ")
wordList = item.split()
for word in wordList:
if validateEmail(word):
listEmail.append(word)
if listEmail:
uniqEmail = set(listEmail)
print(len(uniqEmail), "emails collected!")
writeFile(uniqEmail)
else:
print("No email found.")