import csv from sys import argv if len(argv) != 2: print 'usage: python MBGlab_people.py ' exit() keep_columns = ['abbrev', 'FirstName', 'LastName', 'Status', 'Category', 'JobTitle', 'NetID', 'SkypeID', 'YaleEmail', 'PublicAltEmail', 'PublicCellNumber', 'eRA account' ] def match_columns(header, keep_columns): '''This function takes a set of column names to be kept and a header line. It returns a dictionary of the indices of each column (since these may change over time).''' keep_column_dict = {} for col_name in keep_columns: keep_column_dict[col_name] = header.index(col_name) return(keep_column_dict) with open(argv[1],'r') as f, open('MBGlab_current.txt','w') as outf: reader = csv.reader(f) k = 0 for line in reader: if k == 0: column_dict = match_columns(line, keep_columns) outf.write('\t'.join(keep_columns)+'\n') k += 1 else: out_items = [line[column_dict[col_name]] for col_name in keep_columns] if line[column_dict['Status']] == 'Curr': outf.write('\t'.join(out_items)+'\n')