research/parsing-a-spreadsheet-csv-export
Parsing a Spreadsheet's CSV Export: Quoted Commas, a Byte-Order Mark and Excel Date Serials
Engineering Note · November 20, 2026 · 8 minute read · CSV · Node.js · Python · Data
A spreadsheet’s CSV export can’t be read safely by splitting on commas and newlines. Fields that contain commas, quotes or line breaks are wrapped in double quotes, and the file may start with an invisible byte-order mark that sticks to the first column name. We built a small test file with each of those problems, plus Excel-style date serial numbers, and parsed it four ways. A naive split broke on every awkward row. A real CSV parser in Node.js and in Python handled the quoting, but each one removed the byte-order mark only when told to. Converting date serials needed a fixed epoch in UTC, and below serial 61 our simple formula and two well-known libraries disagreed.
Everything below was run on Linux with Node.js 22.23.5 and csv-parse 7.0.3, Python 3.11.18 with its standard csv module, and, as references for dates, openpyxl 3.1.5 and SheetJS (the xlsx package) 0.18.5. The outputs are the real ones from these runs. This note goes with today’s article, When a Spreadsheet Has Become Software.
What was in the test file?
We could not run Excel or Google Sheets here, so we wrote the file ourselves in the shape spreadsheet exports commonly take: a UTF-8 byte-order mark first, Windows line endings (\r\n), and these rows:
Customer,Address,Booked,Amount,Notes
Ann Lee,"12 Main St, Apt 4",45992,125.50,
"Baker, Tom","9 Elm St",45993.75,"1,200.00","Said ""call first"""
Cara Diaz,4 Oak Ave,45994,80,"Gate code 1234
ring twice"
Each row has five fields. The awkward parts: a comma inside a quoted address, a comma inside a quoted name, a quoted amount with a thousands separator, a doubled "" (the CSV way to write a quote inside a quoted field), a line break inside the last row’s notes, and a booking time stored as a date serial, the number of days since a fixed starting date, with a fraction for the time of day.
The first bytes of the file, shown with od -c:
0000000 357 273 277 C u s t o m e r , A d d r
357 273 277 (in hexadecimal, EF BB BF) is the byte-order mark, U+FEFF, encoded as UTF-8.
What goes wrong with a naive split?
const text = readFileSync('bookings.csv', 'utf8');
const lines = text.split('\n');
const header = lines[0].split(',');
header[0]: "Customer" | length 9 | === "Customer": false
lines: 6
6 fields: ["Ann Lee","\"12 Main St"," Apt 4\"","45992","125.50","\r"]
7 fields: ["\"Baker"," Tom\"","\"9 Elm St\"","45993.75","\"1","200.00\"","\"Said \"\"call first\"\"\"\r"]
5 fields: ["Cara Diaz","4 Oak Ave","45994","80","\"Gate code 1234\r"]
1 fields: ["ring twice\"\r"]
Every problem shows at once. The first header is nine characters long and does not equal "Customer", because the byte-order mark is still on it; it prints the same, so this one is easy to miss. The quoted address and name split in two. Each line keeps a trailing \r. The third row’s notes are cut off, and their second half becomes a row of its own. The third row has five fields, so a check on field count alone would pass it.
Does a real parser fix it in Node.js?
The csv-parse package, with the first line used as column names, and run with and without its bom option:
import { parse } from 'csv-parse/sync';
const buf = readFileSync('bookings.csv');
for (const bom of [false, true]) {
const rows = parse(buf, { columns: true, bom, skip_empty_lines: true });
console.log(`bom: ${bom} -> keys:`, JSON.stringify(Object.keys(rows[0])),
'| rows[0].Customer:', JSON.stringify(rows[0].Customer));
}
bom: false -> keys: ["Customer","Address","Booked","Amount","Notes"] | rows[0].Customer: undefined
bom: true -> keys: ["Customer","Address","Booked","Amount","Notes"] | rows[0].Customer: "Ann Lee"
Without bom: true, every row parsed and row.Customer was undefined, with no error. That is the kind of bug that produces a report with an empty first column. With it, all three rows came back correctly:
{"Customer":"Ann Lee","Address":"12 Main St, Apt 4","Booked":"45992","Amount":"125.50","Notes":""}
{"Customer":"Baker, Tom","Address":"9 Elm St","Booked":"45993.75","Amount":"1,200.00","Notes":"Said \"call first\""}
{"Customer":"Cara Diaz","Address":"4 Oak Ave","Booked":"45994","Amount":"80","Notes":"Gate code 1234\r\nring twice"}
Two things are still left to the caller. Every value is a string, and the line break inside the notes keeps its \r\n. Amounts with a thousands separator need care too:
Number("1,200.00") = NaN | money("1,200.00") = 1200
money here is Number(s.replace(/,/g, '')). That is fine for US-formatted numbers and wrong for a file where the comma is the decimal separator, so check which one you have before stripping anything.
And in Python?
The standard library’s csv.DictReader, with the file opened two ways:
for enc in ('utf-8', 'utf-8-sig'):
with open('bookings.csv', encoding=enc, newline='') as f:
rows = list(csv.DictReader(f))
print(f'encoding={enc} -> first key {list(rows[0])[0]!r} | rows {len(rows)}')
encoding=utf-8 -> first key 'Customer' | rows 3
encoding=utf-8-sig -> first key 'Customer' | rows 3
The same result as Node.js: the quoting was handled either way, and the byte-order mark was removed only with utf-8-sig. The rows matched csv-parse’s exactly, including the \r\n inside the notes.
The usual advice for Python’s csv module is to open files with newline=''. We ran it once without:
without newline="": ['', 'Said "call first"', 'Gate code 1234\nring twice']
No error, but the \r\n inside the quoted cell became \n. For notes that hardly matters. For a field that is compared or hashed later, it is a silent change to the data.
How do you turn a date serial into a date?
For the 1900 date system, a simple formula works for modern dates: add the serial as days to 30 December 1899. Do it in UTC:
const EPOCH = Date.UTC(1899, 11, 30);
const fromSerial = (s) => new Date(EPOCH + Math.round(Number(s) * 86400000));
45992 -> 2025-12-01T00:00:00.000Z
45993.75 -> 2025-12-02T18:00:00.000Z
45994 -> 2025-12-03T00:00:00.000Z
The Python version, datetime(1899, 12, 30) + timedelta(days=float(s)), gave the same three dates and the 18:00 time.
Why UTC?
We ran the same arithmetic with the epoch built in local time, new Date(1899, 11, 30), and printed the result in two time zones:
TZ=America/New_York
45992 -> Mon Dec 01 2025 00:00:00 | toISOString().slice(0,10): 2025-12-01
46100 -> Thu Mar 19 2026 01:00:00 | toISOString().slice(0,10): 2026-03-19
TZ=Europe/Berlin
45992 -> Mon Dec 01 2025 00:00:00 | toISOString().slice(0,10): 2025-11-30
46100 -> Thu Mar 19 2026 00:00:00 | toISOString().slice(0,10): 2026-03-18
In New York the March date came out at 1:00 AM, because the daylight saving change sits between the epoch and that date and adding fixed 24-hour days does not account for it. In Berlin, the common habit of taking the date part of toISOString() gave the day before. A date serial has no time zone. Treat it as UTC from start to finish and format it as UTC.
Where does the simple formula break?
We compared our formula with two libraries that read spreadsheet files, openpyxl in Python and SheetJS in Node.js, for a few small serials and one modern one:
1 ours=1899-12-31 openpyxl=1900-01-01 00:00:00
59 ours=1900-02-27 openpyxl=1900-02-28 00:00:00
60 ours=1900-02-28 openpyxl=1900-02-28 00:00:00
61 ours=1900-03-01 openpyxl=1900-03-01 00:00:00
45992 ours=2025-12-01 openpyxl=2025-12-01 00:00:00
1 ours=1899-12-31 sheetjs=1900-01-01 format=1900-01-01
59 ours=1900-02-27 sheetjs=1900-02-28 format=1900-02-28
60 ours=1900-02-28 sheetjs=1900-02-29 format=1900-02-29
61 ours=1900-03-01 sheetjs=1900-03-01 format=1900-03-01
45992 ours=2025-12-01 sheetjs=2025-12-01 format=2025-12-01
From 61 upward, all three agree. Below 61, both libraries return a date one day later than the formula. At 60 they disagree with each other: SheetJS returns 29 February 1900, a date that never existed, and openpyxl returns 28 February. Both libraries are evidently reproducing a calendar in which serial 60 is an extra day in February 1900, and they handle that day differently. For business data this rarely matters, since nobody books a job in 1900. It does matter when a sheet uses small serials for something else, such as durations, or when a blank date cell exports as 0. Reject serials below 61 as dates unless you know what they mean.
What about the 1904 date system?
Some workbooks use the 1904 date system, which counts from a later starting date. openpyxl can convert either way:
1904 system, 45992 -> 2029-12-02 00:00:00 | difference in days: 1462
The same serial is four years and a day later. A CSV export carries no record of which system the workbook used. If dates come out about four years off, this is the first thing to check.
A checklist for CSV imports
- Use a CSV parser, never
split(','). - Strip the byte-order mark explicitly:
bom: truein csv-parse,encoding='utf-8-sig'in Python. - In Python, open the file with
newline=''. - Check every row has the expected number of fields, and that the header names match what you expect, before importing anything.
- Convert numbers deliberately; know whether the comma is a thousands or a decimal separator.
- Convert date serials from 30 December 1899 in UTC, reject serials below 61, and confirm the workbook’s date system with whoever sends the file.
- Keep the original file. When something looks wrong later, you can re-run the import.
Limitations
- The test file was written by hand to match common export conventions. We could not run Excel, Numbers or Google Sheets here, so we did not check what each one writes; their exports can differ in byte-order marks, line endings, delimiters (some locales use semicolons) and whether dates come out as serials or formatted text.
- Our date reasoning rests on the behavior of openpyxl 3.1.5 and SheetJS 0.18.5. We could not open Microsoft’s documentation on the 1900 and 1904 date systems from this environment.
- Only one parser was tested in each language. Other CSV libraries have their own defaults for byte-order marks and line breaks.
- The time zone results come from Node.js 22.23.5 with its bundled time zone data. Other runtimes may differ for historical dates such as 1899.
Related reading
- When a Spreadsheet Has Become Software
- Comparing Version Strings Without a Library: A 30-Line Semver Compare, Fuzzed Against the Reference
- A 9:30 AM Publish Time That Survives Daylight Saving Changes
Frequently asked questions
Why does the first column of my CSV not match its name?
The file probably starts with a byte-order mark, an invisible character (U+FEFF) some programs write at the start of UTF-8 files. It becomes part of the first header, so a lookup for Customer finds nothing. In Python open the file with encoding utf-8-sig; with the csv-parse package in Node.js pass bom: true.
Why can I not split a CSV line on commas?
Because a field can contain commas, quotes and even line breaks, as long as it is wrapped in double quotes. In our test file, splitting on commas gave rows of 6, 7, 5 and 1 fields where there should have been 5 each. Use a CSV parser, which understands the quoting.
How do I convert an Excel date serial number to a date?
For the 1900 date system, add the serial as days to 30 December 1899, in UTC. Serial 45992 is 1 December 2025, and a fraction such as .75 is the time of day, here 18:00. Serials below 61 need special handling; the libraries we tested return a date one day later than this simple formula.
Why is my converted date one day off?
Three causes showed up in our tests. Building the epoch in local time instead of UTC moved dates by a day in some time zones. Serials below 61 are a day different from the simple formula. A workbook using the 1904 date system is 1,462 days different from one using the 1900 system.
Should I open CSV files with newline='' in Python?
Yes. Opening the file with newline='' lets Python's csv module handle line breaks itself. Without it, in our test the Windows-style line break inside a quoted cell came back as a plain newline, silently changing the stored text.