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

Updated extract_emails.py #272

Merged
merged 2 commits into from
Oct 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions Scripts/Miscellaneous/Email_extractor/extract_emails.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
#!/usr/bin/env python3
import re

print("Enter the name of the input file: ")
file=str(input())
def main():
print("Enter the name of the input file: ")
file=str(input())
return print_emails(get_emails(file))
# Can add os.path functionality

try:
f = open(file,"r")
except FileNotFoundError:
print("File does not exists")
def get_emails(filename:str):
"""Function to return list of email matches found in filename passed"""
with open(filename,'r') as file:
emails=[]
for line in file:
#em = re.find('\S+@\S+\.\S+',line)
#print(em)
regex = re.match(r'\S+@\S+\.\S+',line) # Creates a match object for a correct match
if regex: #if match exists
emails.append(regex.group(0)) # extracts the text of the match

return emails

email={}
def print_emails(emails):
"""Simple printing function"""
for email in emails:
print(email)

for i in f:
em = re.findall('\S+@\S+\.\S+',i)
for j in em:
email[j]=email.get(j,0)+1

f.close()

for i in email:
if(email[i]>=2):
print(i,email[i])
else:
print(i)
if __name__ == "__main__":
main()