Categories
coding Python

Extracting Names from Email Addresses

Given a CSV file with the following format:


;;firstname.lastname@somehost.com

The task is to extract the names from the email addresses. We assume that the names are seperated by periods (.) and that all the names are supposed to be capitalized and printed with strings:


#!/usr/bin/env python

import sys

if len( sys.argv ) < 2:
print "Usage: %s filename" % sys.argv[ 0 ]
sys.exit( 1 )

textFileName = sys.argv[ 1 ]
textFile = open( textFileName, "r" )

for line in textFile:
fields = line.strip().split( ';' )
email = fields[ 2 ].split( "@" )
emailName = email[ 0 ].split( '.' )
capitalizedName = [ x[:1].upper() + x[1:].lower() for x in emailName ]
print '%s;%s;%s' % ( capitalizedName[ 0 ], ' '.join( capitalizedName[ 1: ] ), fields[ 2 ] )

Leave a Reply

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