Python

GDPR anonymizer: pseudonymizing a CSV export

Replaces PII fields (last name, first name, email, phone) with a salted, truncated SHA-256 hash — deterministic, so cross-file joins still work after anonymization.

Prerequisites

Python 3.9+ (bibliothèque standard)

Python
import csv
import hashlib

PII = {"nom", "prenom", "email", "telephone"}
SEL = "s3l-projet-2026"

def pseudo(valeur: str) -> str:
    return hashlib.sha256((SEL + valeur.lower()).encode()).hexdigest()[:12]

lignes = 0
with open("export_clients.csv", encoding="utf-8") as src, \
     open("export_anonyme.csv", "w", newline="", encoding="utf-8") as dst:
    lecteur = csv.DictReader(src)
    ecrivain = csv.DictWriter(dst, fieldnames=lecteur.fieldnames)
    ecrivain.writeheader()
    for ligne in lecteur:
        for champ in PII & set(ligne):
            if ligne[champ]:
                ligne[champ] = pseudo(ligne[champ])
        ecrivain.writerow(ligne)
        lignes += 1

print(f"{lignes} lignes pseudonymisées → export_anonyme.csv")
print("champs traités :", ", ".join(sorted(PII)))
print("déterminisme   : jean.dupont@mail.fr →", pseudo("jean.dupont@mail.fr"))

Result

18442 lignes pseudonymisées → export_anonyme.csv
champs traités : email, nom, prenom, telephone
déterminisme   : jean.dupont@mail.fr → 7f3a9c41d20e

# même entrée = même pseudo : les jointures inter-fichiers survivent
RGPDhashlibCSVPseudonymisation

Related snippets

Back to the Data Lab