Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit 0db85a8

Browse files
authored
Merge pull request #272 from dkarmy12/master
Updated extract_emails.py
2 parents fdc115c + 13cce07 commit 0db85a8

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed
Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
#!/usr/bin/env python3
22
import re
33

4-
print("Enter the name of the input file: ")
5-
file=str(input())
4+
def main():
5+
print("Enter the name of the input file: ")
6+
file=str(input())
7+
return print_emails(get_emails(file))
8+
# Can add os.path functionality
69

7-
try:
8-
f = open(file,"r")
9-
except FileNotFoundError:
10-
print("File does not exists")
10+
def get_emails(filename:str):
11+
"""Function to return list of email matches found in filename passed"""
12+
with open(filename,'r') as file:
13+
emails=[]
14+
for line in file:
15+
#em = re.find('\S+@\S+\.\S+',line)
16+
#print(em)
17+
regex = re.match(r'\S+@\S+\.\S+',line) # Creates a match object for a correct match
18+
if regex: #if match exists
19+
emails.append(regex.group(0)) # extracts the text of the match
20+
21+
return emails
1122

12-
email={}
23+
def print_emails(emails):
24+
"""Simple printing function"""
25+
for email in emails:
26+
print(email)
1327

14-
for i in f:
15-
em = re.findall('\S+@\S+\.\S+',i)
16-
for j in em:
17-
email[j]=email.get(j,0)+1
18-
19-
f.close()
20-
21-
for i in email:
22-
if(email[i]>=2):
23-
print(i,email[i])
24-
else:
25-
print(i)
28+
if __name__ == "__main__":
29+
main()

0 commit comments

Comments
 (0)