/
crelan2gerermescomptes.py
86 lines (71 loc) · 3.74 KB
/
crelan2gerermescomptes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import sys
import csv # For CSV file reading, parsing and writing
import re # For regular expression
if __name__ == '__main__':
verbose = 0
# This script needs one argument: the input file to parse
if len(sys.argv) < 2:
print("Please, specify the filename you want to parse")
print("Example: python %s crelan-export.csv" % sys.argv[0])
sys.exit()
print("Opening file %s..." % sys.argv[1])
csv.register_dialect('myDialect', delimiter=';', quoting=csv.QUOTE_MINIMAL)
with open(sys.argv[1], 'rb') as fin:
with open('out.csv', 'wb') as fout:
reader = csv.reader(fin, dialect='myDialect')
writer = csv.writer(fout, dialect='myDialect')
# Create the header of the output file
header = ['Date de valeur', 'Date operation', 'Montant', 'Contrepartie', "Type d'operation",
'Communication']
writer.writerow(header)
# Display it on the screen
print("Date de valeur;Date de l'operation;Montant;Contrepartie;Type d'operation;Communication")
# Don't parse line #1, this is the header
line = 1
for read_row in reader:
if line != 1:
# Extract useful fields
date_de_valeur = read_row[0]
montant = read_row[1]
contrepartie = read_row[3]
type_operation = read_row[5]
communication = read_row[6]
# Parse the "Communication" field for type
# - "Paiement Bancontact",
# - "Retrait Bancontact",
# - "Paiement Maestro",
# - "Retrait maestro"
#
# ... to extract the transaction date in the "Communication" field
if type_operation == "Paiement Bancontact" or type_operation == "Retrait Bancontact" \
or type_operation == "Paiement Maestro" or type_operation == "Retrait maestro":
# Parse the "Communication" field, looking for the operation date
m = re.search(r'\d{2}-\d{2}-\d{4}', communication)
if m is not None:
date_operation = m.group()
else:
date_operation = "01-01-2050" # Meaning not found"
else:
date_operation = date_de_valeur
# Since May 2017, there is a parsing error for "Paiement Maestro":
# The "contrepartie" field contains the city name
# Let's try to extract the "contrepartie" from the "Communication" field
if type_operation == "Paiement Maestro":
m = re.search(r'\d{16} ', communication)
if m is None:
# This is the new structure (>= May 2017)
# Extract the "contrepartie" from the "Communication" field
contrepartie = communication[:24]
row_to_write = []
row_to_write.append(date_de_valeur)
row_to_write.append(date_operation)
row_to_write.append(montant)
row_to_write.append(contrepartie)
row_to_write.append(type_operation)
row_to_write.append(communication)
writer.writerow(row_to_write)
print("%s;%s;%s;%s;%s;%s" % (date_de_valeur, date_operation, montant, contrepartie, type_operation, communication))
# To parse next line
line += 1
fout.close()
fin.close()