Automatic categorization of a bank statement
Classifies each expense in a CSV statement using regex rules on the label, tallies totals by category and prints the breakdown with proportional bars.
Prerequisites
Python 3.9+ (bibliothèque standard)
Python
import csv
import re
from collections import defaultdict
REGLES = [("CARREFOUR|LIDL|AUCHAN", "courses"),
("SNCF|UBER|TOTALENERGIES", "transport"),
("EDF|ORANGE|FREE TELECOM", "factures"),
("NETFLIX|SPOTIFY|DEEZER", "abonnements")]
totaux, non_classe = defaultdict(float), 0.0
with open("releve_juin.csv", encoding="utf-8") as f:
for ligne in csv.DictReader(f, delimiter=";"):
montant = float(ligne["montant"].replace(",", "."))
if montant >= 0:
continue
for motif, cat in REGLES:
if re.search(motif, ligne["libelle"].upper()):
totaux[cat] += -montant
break
else:
non_classe += -montant
depenses = sum(totaux.values()) + non_classe
print(f"Dépenses de juin : {depenses:.2f} EUR")
for cat, t in sorted(totaux.items(), key=lambda x: -x[1]):
print(f" {cat:<13} {t:>8.2f} " + "▓" * round(t * 24 / depenses))
print(f" {'non classé':<13} {non_classe:>8.2f}")Result
Dépenses de juin : 2184.37 EUR courses 612.40 ▓▓▓▓▓▓▓ factures 498.20 ▓▓▓▓▓ transport 441.85 ▓▓▓▓▓ abonnements 67.97 ▓ non classé 563.95
FinanceCSVRegexCatégorisation