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 | """ |
---|
23 | Fix exports from old SIRP portal to make them importable by current portal. |
---|
24 | |
---|
25 | Usage: |
---|
26 | |
---|
27 | Change into this directory, set the options below (files are assumed |
---|
28 | to be in the same directory) and then run |
---|
29 | |
---|
30 | python fix_import_file.py |
---|
31 | |
---|
32 | Errors/warnings will be displayed on the shell, the output will be put |
---|
33 | into the specified output file. |
---|
34 | """ |
---|
35 | |
---|
36 | ## |
---|
37 | ## CONFIGURATION SECTION |
---|
38 | ## |
---|
39 | # file with input data |
---|
40 | INPUT_FILE = 'students_for_reimport.csv' |
---|
41 | |
---|
42 | # file written with modified output |
---|
43 | OUTPUT_FILE = 'out.csv' |
---|
44 | |
---|
45 | # keys are fieldnames in input file, values are methods of class |
---|
46 | # Converter (see below) |
---|
47 | OPTIONS = { |
---|
48 | 'sex': 'gender', |
---|
49 | 'birthday': 'date', |
---|
50 | 'request_date': 'datetime', |
---|
51 | } |
---|
52 | ## |
---|
53 | ## END OF CONFIG |
---|
54 | ## |
---|
55 | |
---|
56 | import csv |
---|
57 | import datetime |
---|
58 | import sys |
---|
59 | |
---|
60 | class 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 | |
---|
103 | reader = csv.DictReader(open(INPUT_FILE, 'rb')) |
---|
104 | writer = None |
---|
105 | |
---|
106 | for 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 | |
---|
125 | print "Ouput written to %s" % OUTPUT_FILE |
---|