Python Code to Extract Emails by Reading File [Complete Script]
In this tutorial, we will write our own Python script to extract all the email IDs from the given text file. Using this script, you don’t need any external tool to extract emails.
First of all, hope you have Python installed on your system.
Python to extract emails from file:
To make it simple, divide the problem into multiple tasks.
Read each line from the text file.
fileToRead = 'readText.txt' file = open(fileToRead, 'r') listLine = file.readlines()
Related Read: Python code to check if file presents or not
Read each word from the line and save it into the list.
We can use Python split function to get the words from the text line.
fileToRead = 'readText.txt' delimiterInFile = [',', ';'] file = open(fileToRead, 'r') listLine = file.readlines() for itemLine in listLine: item =str(itemLine) for delimeter in delimiterInFile: item = item.replace(str(delimeter),' ')
Note: If you are using
replace() string method, you have to save the result in a new string. Replacing characters from the string in-place is not possible as the string is immutable data type in Python.
Python to Validate / Verify Email ID:
re Python module for pattern matching makes our job easy. Verify each of the string if it is valid email id or not.
import re def validateEmail(strEmail): if re.match("(.*)@(.*).(.*)", strEmail): return True return False
Save all the extracted email IDs in the file.
After validation, save all the valid email IDs into the list
listEmail. Check if all the list items are unique (unique emails IDs). And remove the duplicate email IDs from the list. Save the list into the file
If there is no email in the text file,
listEmail will be empty. Print “No email found.”
For an instance, if you found 40 emails in the file print “4o emails collected!”.
Python Script to Extract Emails from the file:
You can run this code with both the Python 2 and Python 3 version.
Here is Complete Code:
import re fileToRead = 'readText.txt' fileToWrite = 'emailExtracted.txt' delimiterInFile = [',', ';'] def validateEmail(strEmail): # .* Zero or more characters of any type. if re.match("(.*)@(.*).(.*)", 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.")
Most of the code in this Python script is self-explanatory. If you still have doubt, you can ask in the comment section.
The scope of this Python Script
Using Python as a scripting language has its own perk.
Automate Email Marketing: You can use this python script to extract emails from the text file. Many times we need to read all the emails for marketing.
You are ready to automate your email extracting job with this simple Python script.
Extracting emails from the web pages is also simple. Get the source code from the web page using the browser. You can simply use the view-source feature. Example,
Open it in the browser and copy and paste the source code into the file
readEmail.txt. Running this script will give you all the email IDs present on the web page.
You can also use CSV file rather than text file to extract email IDs and to save in it. Using CSV file in Python is pretty simple.
Automation: I use this script to extract the emails IDs of the students subscribed to my Python channel. So that I can import these emails on the email server to send them programming newsletter. It saves my time a lot, rather than adding each individual email ID.
That’s it all form this script written in Python to extract emails from file.
Kindly share, what are the things you have automated using Python? I would like to hear from you.