Review a CSV Import Script for Encoding, Duplicate Rows, and Half-Imported Data

You are a data engineer reviewing a script that imports rows from a CSV or spreadsheet export into a database or API. I will paste the script, plus a few sample rows (anonymized) and a description of where the file comes from.

Review the script for the failure modes below. For each one, say whether the script handles it, handles it badly, or ignores it, and quote the relevant lines.

1. Encoding and format. Byte order marks, non-UTF-8 encodings, mixed line endings, delimiters inside quoted fields, embedded newlines, and a header row that changes name or order between exports.
2. Value parsing. Leading zeros lost in identifiers, numbers formatted with thousands separators or decimal commas, dates in ambiguous day-month order, blank versus "N/A" versus zero, and trailing whitespace.
3. Duplicates and re-runs. What happens if the same file is imported twice, or two files overlap? Is there a natural key or an idempotency mechanism?
4. Partial failure. If row 4,000 of 10,000 fails, what state is the target left in? Is there a transaction, a staging table, or a way to resume?
5. Error reporting. Are bad rows collected with their line numbers and reasons, or does the script fail on the first one or silently skip?
6. Scale and safety. Does it read the whole file into memory, build one giant insert, or lock a table for a long time? Is there a dry-run mode that reports what would change without writing?

Finish with three things: the single highest-risk gap, a minimal patch or pseudocode that fixes it, and five test rows I should add to a fixture file that would expose the other gaps.

Rules: do not assume the file is clean because the sample rows look clean. If a check cannot be answered from the code I pasted, say what you would need to see. Do not rewrite the whole script unless I ask.

## When to use it

Use it before running a one-off migration or a recurring import on real data, and after any import has produced a surprising row count. It is also a good review pass when an import script was written quickly for a single file and is now being reused.

## How to adapt it

Name your target (Postgres, a REST API, a spreadsheet) so the partial-failure advice matches how that system handles transactions and batches. If the file comes from a vendor, paste their format notes. If you work only in a particular language, say so and ask for patches in it.

## Example input and good output

Input: a Python script using csv.reader on open("export.csv"), inserting one row at a time with autocommit, with sample rows including a postal code 02134 and a date 03/04/2024.

A good answer notes that the file is opened without an explicit encoding, that the postal code will be corrupted if cast to int, that 03/04/2024 is ambiguous, and that autocommit leaves a half-imported table after a crash. The highest-risk gap is the lack of idempotency plus partial commits, with a fix using a staging table and an upsert on a natural key. The five fixture rows include a BOM-prefixed header, a quoted field with a newline, a duplicate key, a blank numeric field and the leading-zero postal code.

Sign in to upvote this prompt.