Learning center

Hand over a CSV without changing its values

Send the rules with the file: how NULL is written, the encoding, whether there is a byte order mark, the row count and a hash. A CSV carries none of these, and the receiver's tool will choose.

Four values that change silently#

In the sourceIn the fileA reader may produce
NULLnothing between commasAn empty string
An empty string""NULL, or an empty string
01234 in a text column01234The number 1234
Column id in a file with a BOMbytes EF BB BF, then idA column whose name starts with an invisible character

PostgreSQL keeps the first two apart. A row with an empty string and a NULL, exported with \copy ... csv header from PostgreSQL 18, is:

a,b
"",

Loaded back with the same command, the first value is an empty string and the second is NULL. The COPY documentation gives the rule: in CSV format NULL is by default an unquoted empty string. A spreadsheet that opens and saves the file is likely to write both as nothing, and the distinction is gone.

The BOM is easy to see from code. Reading id,zip with Python's csv module gives the first column name as 'id', so row["id"] raises KeyError while the printed header looks right. Opening the file with encoding="utf-8-sig" removes it.

Python's reader returned 01234 as text. A spreadsheet or a data frame that infers types can drop the zero, and after saving there is no way back.

Send a description next to the file#

sha256sum orders.csv
head -c 3 orders.csv | xxd -p
python3 -c 'import csv,sys; print(sum(1 for _ in csv.reader(open(sys.argv[1], newline="", encoding="utf-8"))) - 1)' orders.csv

efbbbf in the second line is a BOM. The third counts data rows with a CSV parser. wc -l counts newline characters, and it is wrong when a quoted field contains one or when the last line has no newline.

orders.csv, sha256 58ab…, UTF-8, no BOM, comma, header, 20,000 rows. NULL is an unquoted empty field. zip and account are text. Exported 2026-11-24 09:30 UTC from a snapshot, ordered by id.

Check on arrival#

  1. The hash. A different hash means that something rewrote the file. Ask for it again before looking at values.
  2. The row count after loading, compared with the description.
  3. Two counts that a silent change would move: rows where a nullable column is NULL, and the shortest length of a column with leading zeros.

A matching hash confirms that the file arrived unchanged. The destination count can still match if the loader skips one row and loads another twice. When you need to establish that every exported row arrived, compare the source and destination keys and the number of times each occurs. If keys can repeat with different values, compare the full records and their counts. Check the export query separately to establish which rows it selected.

When to choose another format#

If the next step is a load into the same database, use pg_dump for the table, which carries types and NULLs. If it is analysis, a format with types, such as Parquet, avoids inference. CSV is fine when the description travels with it.

Worked example#

This example is fictional. QA reports that 312 customers lost their postal code after an import test. The developer's file has the codes. QA had opened it to look at it and saved it on closing. The hashes differ, so the two know within a minute that they are testing different files. The import was right, and the test is repeated with the original.

JSON has its own version of this, in payloads changed by tools. Return to handing over evidence.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading