How Do You Extract an Email Address from a CSV File?

Problem scenario
You have a CSV file with email addresses. You want to return only the email address -- not lines that have an email address. What do you do?

Assuming the email addresses are in a file named email.txt, use this Python 3 program:

with open('email.txt', 'r') as a:
  for b in a:
    if '@' in b: #operate only on lines with "@"
      c = b.split() #split up words on line
      for d in c: #iterate through characters of words
        if '@' in d: #if email address,
          print(d+';') #print email address

Leave a comment

Your email address will not be published. Required fields are marked *