Saturday, August 18, 2007

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

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

This way mails to or to
will be properly routed back to the U.


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 = mydata[3:]
for row in mydata:
print "" % (clean(row[7]),clean(row[7]))
print "" % (clean(row[2]),clean(row[1]),clean(row[7]))

if __name__ == "__main__":
fp = open(sys.argv[1])
print """

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

No comments: