在匯入失敗前找出 CSV 每列欄位數偏差
Catch CSV row-width drift before it breaks an import
Python 標準庫 csv 可檢查 CSV 每列解析後的欄位數,及早找出可能令批量匯入失敗的列寬偏差;示例中第 3 行有 2 個欄位,第 4 行有 4 個,預期欄位數為 3。CSV 解析器會正確處理引號內的逗號,但欄位數正確不代表金額、必填值或貨幣代碼等資料有效。
Catch CSV row-width drift before it breaks an import
A spreadsheet export can look ordinary but break a bulk import when a line has fewer or more columns than the header. This is a structural problem, separate from missing values or duplicate business identifiers.
Consider this entirely synthetic sample:
order_id,amount,currency
1001,29.50,USD
1002,19.00
1003,"39,00",EUR,EXTRA
1004,10.00,USD
Line 3 has only two fields. Line 4 has four. The comma inside the quoted amount 39,00 belongs to a single field, and the extra field is a separate problem.
Reproducible validation with standard-library Python
import csv
from io import StringIO
sample = """order_id,amount,currency
1001,29.50,USD
1002,19.00
1003,"39,00",EUR,EXTRA
1004,10.00,USD
"""
reader = csv.reader(StringIO(sample), strict=True)
header = next(reader)
width_errors = [
(line_number, len(fields))
for line_number, fields in enumerate(reader, start=2)
if len(fields) != len(header)
]
print("Expected columns:", len(header))
print("Width errors (line, columns):", width_errors)
Expected output:
Expected columns: 3
Width errors (line, columns): [(3, 2), (4, 4)]
What the test does—and what it cannot do
The check counts parsed CSV fields, not literal comma characters. Python's CSV parser respects quoted commas, so do not split a CSV record manually on commas. Row-width validation is useful before trying to map exports to database columns.
Rows with correct field counts may still contain invalid amounts, duplicate business keys, empty required values, or unsupported currency codes. Those are separate data-quality controls. When testing a real import, preserve the original file, confirm encoding and delimiter settings, and check the target importer's mapping before writing production data.
This example uses fabricated data and accesses no client account. You can also explore the free browser-local CSV audit on a small test file.
Disclosure: This technical tutorial was prepared with AI assistance. The Python example was run with synthetic input, and its output was checked before approval for publication.
來源:DEV Community · dev.to