DEV Community

Venture Studios
Venture Studios

Posted on

A Practical, Auditable CSV Cleanup and Deduplication Workflow in Python

Small CSV exports often contain duplicate records, inconsistent headers, blank IDs, and values that break downstream imports. A reliable cleanup pass should preserve the original file, normalize fields, report what changed, and emit data ready for a CRM, database, or JSON API.

Here is a minimal Python pattern for stable deduplication by a chosen business key:

import csv
seen = set()
with open("input.csv", newline="", encoding="utf-8-sig") as src, open("clean.csv", "w", newline="", encoding="utf-8") as dst:
    r = csv.DictReader(src)
    fields = [h.strip().lower().replace(" ", "_") for h in r.fieldnames]
    w = csv.DictWriter(dst, fieldnames=fields); w.writeheader()
    for row in r:
        clean = {(k or "").strip().lower().replace(" ", "_"): (v or "").strip() for k,v in row.items()}
        key = clean.get("email") or clean.get("id")
        if key and key.lower() not in seen:
            seen.add(key.lower()); w.writerow({f: clean.get(f, "") for f in fields})
Enter fullscreen mode Exit fullscreen mode

For one-off exports, edge cases matter more than the snippet: quoted commas, BOMs, duplicate keys with conflicting values, date normalization, required-field reports, and a JSON schema matching the destination system.

I offer a fixed-price $29 CSV-to-JSON cleanup and deduplication turnaround: normalized CSV and JSON output, a duplicate/error report, and a rerunnable Python script. Send a small redacted sample plus the target fields in a comment or Dev.to message to confirm scope before work begins.

The goal is not merely converting a file; it is producing an auditable import that can be rerun next month without spreadsheet surgery.

Top comments (0)