Leave a comment


Python code for extracting email addresses

Python logo

There have been times where I’ve wanted to mass email a bunch of people sharing the same domain name: for example, I’d like to send a notice to everyone I know who attends my school and thus has a @swarthmore.edu email address. Exporting my contacts from Gmail into the Outlook format CSV, I coded the following in Python which outputs a file called output.txt with all the emails you want defined by the line starting with searchquery. Might come in handy.

from string import *

searchquery = "@swarthmore.edu"
lensearch = len(searchquery)
emails = []

f = open("contacts.csv", "r")
g = open("output.txt", "w")
allines = f.readlines()
for i in range(len(allines)):
    splitted = allines[i].split(",")
    if len(splitted) > 2 and \
        splitted[1][-lensearch:] == searchquery:
        g.write(splitted[1] + ", ")

g.close()
f.close()

 

This entry was posted on Tuesday, May 13th, 2008 at 4:52 pm, EST. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.