Every "clean my spreadsheet" job ends with the same two questions, and they are the only ones that matter:
- What exactly did you change to my data?
- Where did you guess?
A cleaner that produces a tidy file but cannot answer those has moved the problem, not solved it. So I built one that answers both, and measured it on a deliberately awful input.
The input
scripts/make_sample.py generates 514 order rows with fixed-seed defects: 34 duplicate order ids (the "export got run twice" classic), 68 dates across three formats, 66 amounts written as " $1,197.00 ", 18 blank amounts, 12 blank emails, padded and mixed-case names, regions and tiers.
The output
rows_read 514
rows_kept 480
duplicates_dropped 34
rows_clean 430
rows_flagged 50 (10.4%)
amount_total_all_parsed 1,135,974.74
amount_total_clean_rows 1,051,120.88
Four sheets: cleaned, needs_review (50 rows, each with an issues reason), audit (every rule with a count), rules (each rule, its action, and why it exists).
The rule that matters: never invent a value
Two cases cannot be resolved from the data:
-
amountis blank — a plausible number would be a lie, so it stays blank and gets flagged. -
05/06/2026— could be May 6 or June 5. When both parts are <= 12 there is no information in the string to decide.
The second one is where most scripts silently pick a convention and corrupt a quarter of the dataset. This one records day-first, stamps date_confidence = ambiguous, and puts the row in needs_review. Your client decides, not the script.
if a > 12 and b <= 12: return date(y, b, a), "dmy"
if b > 12 and a <= 12: return date(y, a, b), "mdy"
if a <= 12 and b <= 12:
# both readings valid -> do not pretend to know
return date(y, b, a), "ambiguous"
Eight assertions that prove nothing was lost
A cleaner is only trustworthy if it can show it did not drop rows quietly. scripts/test_clean.py runs plain asserts, no framework:
- no duplicate
order_idsurvives rows_read == rows_kept + duplicates_dropped- every flagged row appears in
needs_review, and vice versa - every amount is numeric or empty — never a leftover string
- the money total in the workbook equals the total in the report (< 0.01 drift)
- every date is ISO and parseable
- blank amounts == reported
amount_missing - the output is genuinely smaller than the input
{"tests": 8, "passed": 8, "rows_in": 514, "rows_out": 480, "flagged": 50,
"amount_total": 1135974.74}
The reconciliation assertions are the point. rows_read == rows_kept + duplicates_dropped catches the classic bug where a dedupe step quietly swallows rows that were not duplicates — the failure mode nobody reviews because the output looks tidy.
Why an audit sheet beats a faster script
A month later, nobody remembers the rules. The audit sheet is the difference between "I think it removed duplicates" and "it dropped 34 duplicate order ids; here are their source rows." It also makes re-runs meaningful: same input, same workbook, and the audit tells you if anything shifted.
Code, sample input and the produced workbook: https://github.com/gougousongsong/csv-cleanup-proof
The cleaner is plain Python (csv + openpyxl), no framework. Adapting it to a new client file usually means editing three things: the column names, the amount/date parsers, and the rules text. The audit and review machinery stays exactly as it is — that is the part people actually keep asking for.
If you'd rather not wire this up yourself: the exact cleaner + rules file + audit template from this post are packaged as a $9.99 CLI & audit toolkit (runs offline, plain Python, no account). Want to see the output format before paying? The free 3-rule mini sample produces the same two sheets on 20 rows.
Top comments (0)