Python program to extract Email-id from URL text file
Prerequisite : Pattern Matching with Python Regex
Given the URL text-file, the task is to extract all the email-ids from that text file and print the urllib.request library can be used to handle all the URL related work.
Example :
Input : Hello This is Geeksforgeeks review-team@geeksforgeeks.org contribute@geeksforgeeks.org GfG is a portal for geeks feedback@geeksforgeeks.org careers@geeksforgeeks.org Output : [] [] ['review-team@geeksforgeeks.org'] ['contribute@geeksforgeeks.org'] [] ['feedback@geeksforgeeks.org'] ['careers@geeksforgeeks.org']
URL text file can be handled using urllib.request. For extracting the emails using regular expressions, re library can be used. For more details of Regular Expression, refer this.
# library that handles the URL stuff import urllib.request # Importing module required for # regular expressions import re # Assign urlopen to a file object variable fhand = urllib.request.urlopen for line in fhand: # Getting the text file # content line by line. s = line.decode().strip() # regex for extracting all email-ids # from the text file reg = re.findall(r"[A-Za-z0-9._%+-]+" r"@[A-Za-z0-9.-]+" r"\.[A-Za-z]{2,4}", s) # printing the list output print(reg) |
Output :
[] [] ['review-team@geeksforgeeks.org'] ['contribute@geeksforgeeks.org'] [] ['feedback@geeksforgeeks.org'] ['careers@geeksforgeeks.org']
Recommended Posts:
- Python | Scramble words from a text file
- Python | Count occurrences of each word in given text file (Using dictionary)
- Python | Create an empty text file with current date as its name
- Python | Program to extract frames using OpenCV
- Python Program to delete a file
- numpy.extract() in Python
- reStructuredText | .rst file to HTML file using Python for Documentations
- Python | Extract digits from given string
- Python | Extract numbers from string
- Python | Extract URL from HTML using lxml
- Python | Pandas Series.str.extract()
- Python | Extract words from given string
- Python | Extract key-value of dictionary in variables
- Python | Extract only characters from given string
- Extract images from video in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



