A naive CSV export split one product title into two columns the moment the title had a comma
A real CSV parser read 4 columns for that row instead of the 3 every other row had
Python's own csv.writer fixed it with zero extra code, just swapping a join for a library call
The bug only shows up once, when someone names a product with a comma or a quote in it
I found this one by accident, testing an export script with a product title that happened to have a comma in it. The naive version, the one built with a plain string join because it's three lines shorter, silently shifted every column after the title one slot to the right. The proper version, built with Python's csv module instead of a join, didn't.
The Export That Looked Fine Until One Row Didn't
Most CSV exports start the same lazy way. You've got a list of rows, each one a dictionary or an object, and instead of reaching for a library you just join the fields with commas, because it's one line and it works on your test data.
products = [
{"sku": "SKU-104", "title": "Starter Hoodie", "price": "18.00"},
{"sku": "SKU-212", "title": "Studio Tee, Black Edition", "price": "24.00"},
{"sku": "SKU-331", "title": 'The "Night Shift" Hoodie', "price": "42.00"},
]
lines = ["sku,title,price"]
for p in products:
lines.append(f'{p["sku"]},{p["title"]},{p["price"]}')
naive_csv = "\n".join(lines)
That function ran clean on the first row. It ran clean on hundreds of rows, if your test data never happened to include a comma. The second product in my list breaks it, because "Studio Tee, Black Edition" has a comma sitting right inside the field the join function has no idea is supposed to stay together.
I fed the naive output back through a real CSV parser to see exactly what a downstream tool, a spreadsheet import, an analytics pipeline, anything that actually reads CSV instead of eyeballing it, would see:
import csv, io
reader = csv.reader(io.StringIO(naive_csv))
for row in reader:
print(row)
The first row parsed as 3 columns, exactly as intended. The second row parsed as 4 columns. "Studio Tee" landed in the title column, "Black Edition" landed in a column that doesn't exist in the header, and the price that was supposed to be in column 3 was still there, just reported under the wrong header once a tool tried to line it up against sku,title,price.
Why This Kind of Bug Hides So Well
Here's the part that makes this bug worse than a normal one: it doesn't fail loudly, and it doesn't fail on every row. It fails on exactly the rows where a title happens to contain the one character the export forgot to escape. Everything else looks completely fine, including in a quick manual check, because you're not going to open a 500-row export and eyeball every line for stray commas.
I tested a second case that's even sneakier: a title with a literal quote character in it, The "Night Shift" Hoodie. A naive join doesn't touch quote characters at all, so it writes the field as-is, with bare double quotes sitting inside an unquoted CSV field. Some parsers tolerate that. Others choke entirely, or silently drop the quote marks, which turns "Night Shift" into Night Shift with no indication anything changed.
This is the same class of bug as a SQL injection that only triggers on an apostrophe in someone's last name, or a template that breaks on an email address with a plus sign in it. The code path works for every piece of test data you thought to write, and then a real piece of user-generated text shows up with a character your test data never had, and the bug ships quietly until someone downstream notices a price in the wrong column.
There's a worse version of the same bug. I tested a title with a literal newline in it, something a copy-paste from another document can easily introduce without anyone noticing. The naive join doesn't just misplace a column this time, it splits one product into two entire rows:
products = [
{"sku": "SKU-104", "title": "Starter Hoodie", "price": "18.00"},
{"sku": "SKU-450", "title": "Weekend Tee\nSecond Line Variant", "price": "20.00"},
]
Reading that naive output back, I got 4 rows out of 2 products plus a header, which should have been 3 rows total. Row 3 was ['SKU-450', 'Weekend Tee'], cut off mid-title with no price at all. Row 4 was ['Second Line Variant', '20.00'], a two-column row that looks like a completely separate, broken product with no SKU. A comma shifts columns. A newline manufactures a phantom row out of thin air, one that any downstream count-the-rows check would silently accept as real data.
For a store export specifically, this shows up most often in Shopify's own product CSV exports, where a title with a comma is genuinely common ("Tee, Black" is a completely normal product name), so any custom export script layered on top of Shopify's own data needs to assume commas and quotes will appear in real titles, not treat them as an edge case.
The One-Line Fix That Isn't Actually a Hack
The fix isn't a regex. It isn't manually checking every field for a comma before deciding whether to wrap it in quotes, either.
That path has its own edge cases, like a field that already contains a quote character, which needs to be escaped as two quote characters, not one. The fix is to stop writing CSV by hand and let a library that already solved this do it:
import csv, io
buf = io.StringIO()
writer = csv.writer(buf)
writer.writerow(["sku", "title", "price"])
for p in products:
writer.writerow([p["sku"], p["title"], p["price"]])
proper_csv = buf.getvalue()
Running the same read-back test against this output, every row parsed as exactly 3 columns. The comma in "Studio Tee, Black Edition" came back whole, because csv.writer automatically wrapped that field in quotes: "Studio Tee, Black Edition". The quote character in "Night Shift" came back correctly too, doubled up as ""Night Shift"" inside the wrapping quotes, which is the actual CSV spec's answer to "how do you put a quote inside a quoted field."
The newline case fixed itself too, the same way. "Weekend Tee" and "Second Line Variant" came back as one field, Weekend Tee\nSecond Line Variant, inside a single properly quoted row. No phantom fourth row, no SKU-less second entry. The writer wrapped the whole field in quotes the moment it saw the embedded newline, which is exactly what the format expects and exactly what I'd never have written by hand without looking it up first.
I didn't write any of that quoting logic myself. That's the point. csv.writer decided, field by field, whether a value needed quoting, and it got every row right including the two that broke the naive version. The same module has a DictWriter that skips the manual writerow calls entirely if your data is already a list of dictionaries, which is the shape most exports start in anyway.
with open("export.csv", "w", newline="") as f:
writer = csv.DictWriter(f, fieldnames=["sku", "title", "price"])
writer.writeheader()
writer.writerows(products)
JavaScript doesn't ship a CSV writer in the standard library the way Python does, but the fix is the same shape: reach for a small, well-tested library like csv-stringify or papaparse instead of a template string with commas in it. The specific tool matters less than refusing to hand-roll the quoting rules yourself.
What I Actually Changed After Finding This
I went back through every export script I'd written by hand, the quick ones meant to dump a report or a product list into a spreadsheet, and found three that used a plain join instead of a real CSV writer. None of them had broken yet, because none of them had been fed a title with a comma in it during the time I'd been using them.
That's not the same as them being correct. It's a bug that hasn't found its trigger yet.
All three got rewritten with csv.DictWriter the same afternoon. It took less time than writing this post did.
I'm also treating "does this handle a comma and a quote in the data" as a standing test case any time I write something that touches CSV, the same reflex as checking what happens when a form field is empty or a number is negative. The same instinct applies to catching bugs before a file ever reaches a real audience, where the actual failure only shows up once you test the input you didn't expect instead of the one you planned for.
The deeper habit this reinforced: any time I'm building a text format by joining strings with a delimiter, whether that's CSV, a pipe-separated log line, or a custom config format, I ask whether the delimiter character can ever appear inside one of the values. If the answer is yes, and it almost always is once real data shows up, hand-rolling the format is building the exact bug this post is about, just with a different delimiter character.
Bottom Line
A plain string join works right up until one value contains the character you're joining on, and CSV's delimiter is a comma, which shows up in real product titles constantly. The fix cost zero extra lines of logic, just swapping ",".join(...) for a library call that already knows the quoting rules. Test every export with one deliberately annoying value, a comma, a quote, a newline inside a field, before you trust it with real data.
What's the one character your own export script has never actually been tested against?
This article contains affiliate links. If you sign up through them, I may earn a small commission at no extra cost to you. (Ad)
Top comments (0)