serhiy-storchaka · GitHub

Bug report

csv.Sniffer detects skipinitialspace by a simple rule: the spaces are a part of the format if every field which follows a delimiter starts with a space. It is wrong in both directions.

A space before a quoted field is not detected. A quote is only a quote at the very start of a field, so not skipping the space splits the field:

>>> sample = 'a, "b,c"\nd,e\nf,g\n'
>>> dialect = csv.Sniffer().sniff(sample)
>>> dialect.skipinitialspace
False
>>> list(csv.reader(io.StringIO(sample), dialect))
[['a', ' "b', 'c"'], ['d', 'e'], ['f', 'g']]

A space which cannot be skipped is counted as evidence. Spaces inside quoted or escaped fields are never skipped, so they say nothing about the padding:

>>> csv.Sniffer().sniff('a," b"\nc, d\n').skipinitialspace
True
>>> csv.Sniffer().sniff('a,\\ b\\,c\nd, e\n').skipinitialspace
True

In both samples the first field is not padded at all, so the sample gives evidence against the padding, not for it.

Linked PRs

Read the original on github.com ↗