fix: auto-detect single-column CSV files

Format detection passed the first bytes to csv.Sniffer().sniff(), which
raises csv.Error('Could not determine delimiter') for files with no
delimiter at all, so a bare single-column list like 'id\n1\n2' could
never be loaded through rows_from_file. Fall back to the default CSV
dialect when sniffing fails so DictReader treats the file as a single
column.
This commit is contained in:
Harsh Thakkar 2026-09-24 05:54:53 +05:30
commit 04c67240ac
2 changed files with 29 additions and 4 deletions

View file

@ -389,14 +389,24 @@ def rows_from_file(
with buffered:
return rows_from_file(buffered, format=Format.JSON)
else:
dialect = csv.Sniffer().sniff(
first_bytes.decode(encoding or "utf-8-sig", "ignore")
)
try:
dialect = csv.Sniffer().sniff(
first_bytes.decode(encoding or "utf-8-sig", "ignore")
)
except csv.Error:
# Single-column files have no delimiter to sniff (e.g. a bare
# "id\n1\n2\n" list). Fall back to the default CSV dialect so
# DictReader still loads them as a single column.
dialect = None
rows, _ = rows_from_file(
buffered, format=Format.CSV, dialect=dialect, encoding=encoding
)
# Make sure we return the format we detected
detected_format = Format.TSV if dialect.delimiter == "\t" else Format.CSV
detected_format = (
Format.TSV
if dialect is not None and dialect.delimiter == "\t"
else Format.CSV
)
return (
_extra_key_strategy(
cast(Iterable[dict[str | None, object]], rows),