#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys, re, csv

re_not = re.compile(r'Not (available|loaded)')
re_time = re.compile(r' [0-2][0-9]:[0-5][0-9]:[0-5][0-9]$')
re_ano = re.compile(ur'(Don( récurrent)? de )(\w)\S+( (\w).*)?$', re.I)
sub_ano = lambda x: x.group(1) + (x.group(3) + "." + x.group(5) + ".").upper()
ano = lambda x: re_ano.sub(sub_ano, x)
re_creditmut = re.compile(r'(SEPA ONLINE SAS) ONL (\d+)( DEDIBOX \2)')
def process_data(data):
    writer = csv.writer(sys.stdout)
    for line in data:
        if not line:
            continue
        if line[0] == 'id':
            writer.writerow(line)
            continue
        for i, el in enumerate(line):
            line[i] = re_not.sub('', line[i].decode('utf-8'))
            line[i] = line[i].replace(u'Paiement récurrent de ', u'Don récurrent de ')
            line[i] = line[i].replace(u'Paiement de ', u'Don de ')
            line[i] = ano(line[i])
            line[i] = re_creditmut.sub(r'\1\3', line[i])
        line[0] = re.sub(r'^[0-9A-Z]*@', '', line[0])
        line[2] = re_time.sub('', line[2])
        line[3] = re_time.sub('', line[3])
        if line[11] and line[11] not in ['commission', '0.00']:
            line[8] += (u' (€%.2f)' % (float(line[9]) - float(line[11]))).replace('.', ',')
        # Only keep lines with definitive date to avoid duplicates across multiple dates
        if len(line[2]) == 10:
            writer.writerow([el.encode('utf-8') for el in line])

if __name__ == "__main__":
    with open(sys.argv[1]) as f:
        process_data(csv.reader(f, delimiter=";"))

@@ -61,16 +61,72 @@
 e, csv%0A%0A
+ANON = %7B%0A  %22@creditmutuel;2017-12-29%22: %22Don de S.F.%22%0A%7D%0A%0A
 re_not =
@@ -684,32 +684,77 @@
        continue%0A
+        check = %22%25s;%25s%22 %25 (line%5B0%5D, line%5B2%5D)%0A
         for i, e
@@ -1075,32 +1075,126 @@
 %5C1%5C3', line%5Bi%5D)%0A
+        if check in ANON:%0A            line%5B6%5D = ANON%5Bcheck%5D%0A            line%5B8%5D = ANON%5Bcheck%5D%0A
         line%5B0%5D 

add manual anon for virement credit mut