Split AI CSV only after a parser reads a row you know

Write one known row with a comma inside a field. Keep the file only when csv.reader returns those three fields.

Published 2026-12-02 · Translation: VI

In short

  • Write one known row before you read the chat CSV — including a field that contains a comma.
  • Parse with a real CSV reader (csv.reader in Python, or an equivalent), not line.split(',').
  • Keep the file only when that row comes back with the same field count and values.

Example (made up): a shop line is sku, name, qty. The name is Gao ST25, 5kg. The RFC line is:

RICE-5KG,"Gao ST25, 5kg",2

Same idea as checking JSON with a strict parser, or a regex on rows you already marked.

Write the row you will accept

RFC 4180 says fields that contain commas, quotes, or line breaks should be enclosed in double quotes, and a quote inside a quoted field is written as two quotes. It also says every record should have the same number of fields.

Your sheet is three fields. Write them first:

Field Value
sku RICE-5KG
name Gao ST25, 5kg
qty 2

If the chat drops the quotes around the name, a comma split invents a fourth column. That is not a small formatting quirk — every later column slides.

Known fields sku, name with an inner comma, and qty, plus the RFC line with quotes around the name.

The comma in Gao ST25, 5kg is data. Quotes keep it in field 2.

split(',') is not a CSV parser

Checked on Python 3.15.2 (2 December 2026).

import csv, io

line = 'RICE-5KG,"Gao ST25, 5kg",2\n'
print(list(csv.reader(io.StringIO(line)))[0])
# ['RICE-5KG', 'Gao ST25, 5kg', '2']

print(line.strip().split(','))
# ['RICE-5KG', '"Gao ST25', ' 5kg"', '2']

Without quotes in the text, the naive split is worse:

print('RICE-5KG,Gao ST25, 5kg,2'.split(','))
# ['RICE-5KG', 'Gao ST25', ' 5kg', '2']

Four fields. qty is no longer field 3. Spreadsheet imports that “succeed” with shifted columns are the expensive failure — the file opens, the numbers look real, and they belong to the wrong headers.

A doubled quote inside the name is valid RFC CSV:

print(list(csv.reader(io.StringIO('RICE-5KG,"Gao ""ST25"", 5kg",2\n')))[0])
# ['RICE-5KG', 'Gao "ST25", 5kg', '2']

A bare quote mid-field is not. Default csv.reader can still return four wrong pieces; strict=True raises csv.Error: ',' expected after '"'.

csv.reader returns three fields matching the sheet; line.split returns four and slides qty.

Field count matching is not enough if values moved. Compare to the sheet.

Ask, then parse with strict=True

Return one CSV record and nothing else.
Fields I already wrote, in order:
sku = RICE-5KG
name = Gao ST25, 5kg
qty = 2
Quote any field that contains a comma or a quote.
A quote inside a quoted field must be doubled.
I will read with Python csv.reader(..., strict=True).
Wrong field count or wrong values fails.
import csv, io

KNOWN = ["RICE-5KG", "Gao ST25, 5kg", "2"]

def accept(text: str) -> bool:
    rows = list(csv.reader(io.StringIO(text), strict=True))
    return len(rows) == 1 and rows[0] == KNOWN

Open files with newline='' when you read from disk — the Python csv docs require it so embedded newlines stay intact. Do not use eval on CSV. Do not “fix” a shifted row by deleting a comma by hand unless you re-run the known-row check.

Write three fields, ask for CSV, parse with strict csv.reader, keep only on an exact match.

If the parse fails or the row differs, ask again from the sheet — do not edit the chat text until it “looks right.”

Share

X Facebook LinkedIn
Educational content only. Rankings reflect stated criteria at the update date—not investment advice or permanent performance guarantees.