Python script to move from CSV to sendmail alias format

Every now and then, I’ll try to post snippets of Python code. Python is the language that I (mis)use most often. These are examples of hobbyist-level code. Also, there is a high probability that the code below will be munged up somewhat by the weblogging software.


#!/usr/bin/env python
"""
============
mkaliases.py
============

Ben Chapman - last update 8/2007

Creates a list on standard out suitable for newaliases from a comma separated
file generated from the OPUS report 'Netids of Term Activated Stds', which is
the one that we normally get from Arnita.

For each input line, two output lines are created in the form

NetID:NetID@emory.edu
fname.lname:fname.lname@emory.edu

This way mails to NetID@law.emory.edu or to firstname.lastname@law.emory.edu
will be properly routed back to the U.

-----
TO DO
-----

Does not currently check for duplicate LHS expressions, which is an error.
clean() should be re-written as a single regex and then it should be applied
via map or some other fanciness.

"""

import csv,sys

def clean(string):
""" Do some crude clean up on the data in the CSV file by removing spaces,
periods, commas, and apostrophes. Lower-case result and return.
"""
string = string.replace(' ','')
string = string.replace('.','')
string = string.replace(',','')
string = string.replace("'",'')
string = string.lower()
return string


def process():
mydata = []
reader = csv.reader(open(sys.argv[1], "rU"))
for row in reader:
mydata.append(row)
mydata = mydata[3:]
for row in mydata:
print "%s:%s@emory.edu" % (clean(row[7]),clean(row[7]))
print "%s.%s:%s@emory.edu" % (clean(row[2]),clean(row[1]),clean(row[7]))

if __name__ == "__main__":
try:
fp = open(sys.argv[1])
except:
print """
Usage: %s NAMEOFCSVFILE >NAMEOFOUTPUTFILE

Input file is a csv version of the TERM ID report from Opus.
""" % (sys.argv[0])
sys.exit(1)
process()

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s