A Shopify product import that fails usually says so in one line, after the upload. Shopify's help center lists the common messages and their causes (Solutions to common product CSV import problems). Several of them are caused by the file itself. These three can be found on your own machine before uploading:
| Message or symptom | Cause (summarised from Shopify's help) |
|---|---|
| Strange characters in descriptions | The file is not saved as UTF-8 |
Ignored line ... because it did not contain product data |
An illegal character or a stray quote in the line |
Validation failed: Options are not unique |
Two variants of one product have the same option values |
The others on that page (product category, metafield product references, file size) depend on your store or on Shopify's taxonomy, and a local script cannot settle them.
The script
Standard library only, Python 3.9 or later. It reads the file once and prints the record number for each problem.
"""Pre-check a Shopify product CSV before importing it (standard library only).
python shopify_csv_precheck.py products.csv exit 0 = nothing found, 1 = problems listed, 2 = cannot read
Finds three causes behind common import errors: a file that is not UTF-8 ("strange characters"),
a broken quote that swallows the following lines ("did not contain product data"), and two variants of one
product with the same option values ("Options are not unique"). It does not check categories, metafields or images.
"""
import csv
import io
import sys
OPTIONS = ("option1 value", "option2 value", "option3 value") # matched case-insensitively
def check(raw):
try:
text = raw.decode("utf-8-sig")
except UnicodeDecodeError as e:
return [f"not UTF-8 (byte {e.start}): save as 'CSV UTF-8' and import again"]
rows = list(csv.reader(io.StringIO(text, newline="")))
if not rows:
return ["empty file"]
head, problems = rows[0], []
if text.count('"') % 2:
problems.append('odd number of " characters: a quote is not closed (stricter CSV readers will run into the next lines)')
for n, row in enumerate(rows[1:], start=2):
if len(row) != len(head):
problems.append(f"record {n}: {len(row)} cells, header has {len(head)}")
col = {name.strip().lower(): i for i, name in enumerate(head)}
handle = col.get("handle", col.get("url handle"))
opts = [col[o] for o in OPTIONS if o in col]
if handle is None or not opts:
return problems
seen, current = {}, None
for n, row in enumerate(rows[1:], start=2):
if len(row) != len(head):
continue
current = row[handle].strip() or current
values = tuple(row[i].strip() for i in opts)
if not any(values):
continue
first = seen.setdefault((current, values), n)
if first != n:
problems.append(f"record {n}: '{current}' repeats option values {values} from record {first} (Options are not unique)")
return problems
def main(path):
try:
raw = open(path, "rb").read()
except OSError as e:
print(f"cannot read: {e}")
return 2
problems = check(raw)
for p in problems:
print(p)
print("no problems found" if not problems else f"{len(problems)} problem(s)")
return 1 if problems else 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1]) if len(sys.argv) == 2 else 2)
Two choices worth explaining:
-
Header names are matched case-insensitively. Older exports use
HandleandOption1 Value; the current template usesURL handleandOption1 value. Both appear in real files. - Variant rows inherit the handle above them when their own handle cell is empty, so a duplicate is found inside the product it belongs to, not across the whole file.
Run on five planted files
I wrote one problem into each small CSV and ran the script (Python 3.13.12):
| File | Planted problem | Output | Exit |
|---|---|---|---|
| 1 | Two tee rows with Size=M, Color=Black (old headers) |
record 3: 'tee' repeats option values ('M', 'Black') from record 2 (Options are not unique) |
1 |
| 2 | Title Cap "Blue (quote never closed) |
odd number of " characters: a quote is not closed (stricter CSV readers will run into the next lines) |
1 |
| 3 | Saved as Windows-1252 (Café mug) |
not UTF-8 (byte 101): save as 'CSV UTF-8' and import again |
1 |
| 4 | Clean file, sizes M and L | no problems found |
0 |
| 5 | File 1 with the current headers (URL handle, Option1 value) |
same message as file 1 | 1 |
A missing file prints cannot read: ... and exits with 2, so the script can sit in a batch job and stop it.
File 2 is why the script counts quotes instead of trusting the parser: Python's csv module is lenient and reads a quote in the middle of a field as an ordinary character, so every record in file 2 still has 8 cells and the cell-count check stays silent. A stricter parser treats the same quote as the start of a quoted cell and keeps reading into the next line; Shopify's help names a stray quote as one cause of skipped lines.
Where it falls short
- The quote check is a count. A file with two separate broken quotes has an even count and can pass unnoticed.
- It does not know Shopify's column list, so a misspelt header is not reported (Shopify shows the "Choose column headings" window for that).
- A product without options that has two variant rows under one handle (Shopify reports
The variant 'Default Title' already exists) is not reported: the duplicate check only compares rows that have option values. - Product category, metafield references, image URLs and the 15 MB size limit are not checked.
If you edit the CSV in Excel, saving as CSV UTF-8 (Comma delimited) instead of plain CSV (Comma delimited) avoids the first problem on most systems.
This article was created with the help of AI (Claude Code). The script and every output above were run and checked before publishing.
Top comments (0)