## ## fix_import_file.py ## Login : ## Started on Wed Jan 25 17:08:30 2012 Uli Fouquet ## $Id$ ## ## Copyright (C) 2012 Uli Fouquet ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## """ Fix exports from old SIRP portal to make them importable by current portal. Usage: Change into this directory, set the options below (files are assumed to be in the same directory) and then run python fix_import_file.py Errors/warnings will be displayed on the shell, the output will be put into the specified output file. """ ## ## CONFIGURATION SECTION ## # file with input data INPUT_FILE = 'students_for_reimport.csv' # file written with modified output OUTPUT_FILE = 'out.csv' # keys are fieldnames in input file, values are methods of class # Converter (see below) OPTIONS = { 'sex': 'gender', 'birthday': 'date', 'request_date': 'datetime', 'marit_stat': 'marit_stat', 'entry_session': 'session', 'current_session': 'session', } # Mapping input file colnames --> output file colnames COLNAME_MAPPING = { 'jamb_reg_no': 'reg_no', 'birthday': 'date_of_birth', } ## ## END OF CONFIG ## import csv import datetime import sys def convert_fieldnames(fieldnames): """Replace input fieldnames by fieldnames of COLNAME_MAPPING. """ header = dict([(name, name) for name in fieldnames]) for in_name, out_name in COLNAME_MAPPING.items(): if in_name not in header: continue header[in_name] = out_name return header class Converters(): """Converters to turn old-style values into new ones. """ @classmethod def session(self, value): """ '08' --> '2008' """ try: number = int(value) except ValueError: return 9999 if number < 14: return number + 2000 elif number in range(2000,2015): return number else: return 9999 @classmethod def marit_stat(self, value): """ 'True'/'False' --> 'married'/'unmarried' """ if value == 'True': value = 'married' elif value == 'False': value = 'unmarried' else: value = '' return value @classmethod def gender(self, value): """ 'True'/'False' --> 'female'/'male' """ if value == 'True': value = 'female' elif value == 'False': value = 'male' else: value = '' return value @classmethod def date(self, value): """ 'yyyy/mm/dd' --> 'yyyy-mm-dd' """ if value == "None": value = "" elif value == "": value = "" else: value = value.replace('/', '-') # We add the hash symbol to avoid automatic date transformation # in Excel and Calc for further processing value += '#' return value @classmethod def datetime(self, value): """ 'yyyy/mm/dd' --> 'yyyy-mm-dd' """ #print "IN: ", value if value == "None": value = "" elif value == "": value = "" else: #value = datetime.datetime.strptime(value, '%Y/%m/%d') #value = datetime.datetime.strftime(value, '%Y-%m-%d') pass #print "OUT: ", value return value reader = csv.DictReader(open(INPUT_FILE, 'rb')) writer = None for num, row in enumerate(reader): if num == 0: writer = csv.DictWriter(open(OUTPUT_FILE, 'wb'), reader.fieldnames) print "FIELDS: " for x, y in enumerate(reader.fieldnames): print x, y header = convert_fieldnames(reader.fieldnames) writer.writerow(header) for key, value in row.items(): if not key in OPTIONS.keys(): continue conv_name = OPTIONS[key] converter = getattr(Converters, conv_name, None) if converter is None: print "WARNING: cannot find converter %s" % conv_name continue row[key] = converter(row[key]) writer.writerow(row) print "Ouput written to %s" % OUTPUT_FILE