source: main/waeup.sirp/trunk/tools/fix_import_file.py @ 7512

Last change on this file since 7512 was 7512, checked in by uli, 13 years ago

Add some helper tool for fixing old-style export files.

File size: 3.4 KB
Line 
1##
2## fix_import_file.py
3## Login : <uli@pu.smp.net>
4## Started on  Wed Jan 25 17:08:30 2012 Uli Fouquet
5## $Id$
6##
7## Copyright (C) 2012 Uli Fouquet
8## This program is free software; you can redistribute it and/or modify
9## it under the terms of the GNU General Public License as published by
10## the Free Software Foundation; either version 2 of the License, or
11## (at your option) any later version.
12##
13## This program is distributed in the hope that it will be useful,
14## but WITHOUT ANY WARRANTY; without even the implied warranty of
15## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16## GNU General Public License for more details.
17##
18## You should have received a copy of the GNU General Public License
19## along with this program; if not, write to the Free Software
20## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21##
22"""
23Fix exports from old SIRP portal to make them importable by current portal.
24
25Usage:
26
27Change into this directory, set the options below (files are assumed
28to be in the same directory) and then run
29
30  python fix_import_file.py
31
32Errors/warnings will be displayed on the shell, the output will be put
33into the specified output file.
34"""
35
36##
37## CONFIGURATION SECTION
38##
39# file with input data
40INPUT_FILE = 'students_for_reimport.csv'
41
42# file written with modified output
43OUTPUT_FILE = 'out.csv'
44
45# keys are fieldnames in input file, values are methods of class
46# Converter (see below)
47OPTIONS = {
48    'sex': 'gender',
49    'birthday': 'date',
50    'request_date': 'datetime',
51    }
52##
53## END OF CONFIG
54##
55
56import csv
57import datetime
58import sys
59
60class Converters():
61    """Converters to turn old-style values into new ones.
62    """
63    @classmethod
64    def gender(self, value):
65        """ 'True'/'False' --> 'female'/'male'/''
66        """
67        if value == 'True':
68            value = 'female'
69        elif value == 'False':
70            value = 'male'
71        else:
72            value = ''
73        return value
74
75    @classmethod
76    def date(self, value):
77        """ 'yyyy/mm/dd' --> 'yyyy-mm-dd'
78        """
79        if value == "None":
80            value = ""
81        elif value == "":
82            value = ""
83        else:
84            value = value.replace('/', '-')
85        return value
86
87    @classmethod
88    def datetime(self, value):
89        """ 'yyyy/mm/dd' --> 'yyyy-mm-dd'
90        """
91        #print  "IN: ", value
92        if value == "None":
93            value = ""
94        elif value == "":
95            value = ""
96        else:
97            #value = datetime.datetime.strptime(value, '%Y/%m/%d')
98            #value = datetime.datetime.strftime(value, '%Y-%m-%d')
99            pass
100        #print "OUT: ", value
101        return value
102
103reader = csv.DictReader(open(INPUT_FILE, 'rb'))
104writer = None
105
106for num, row in enumerate(reader):
107    if num == 0:
108        writer = csv.DictWriter(open(OUTPUT_FILE, 'wb'), reader.fieldnames)
109        print "FIELDS: "
110        for x, y in enumerate(reader.fieldnames):
111            print x, y
112        header = dict([(x, x) for x in reader.fieldnames])
113        writer.writerow(header)
114    for key, value in row.items():
115        if not key in OPTIONS.keys():
116            continue
117        conv_name = OPTIONS[key]
118        converter = getattr(Converters, conv_name, None)
119        if converter is None:
120            print "WARNING: cannot find converter %s" % conv_name
121            continue
122        row[key] = converter(row[key])
123    writer.writerow(row)
124
125print "Ouput written to %s" % OUTPUT_FILE
Note: See TracBrowser for help on using the repository browser.