Skip to content

csv.Sniffer misdetects skipinitialspace #154744

Description

@serhiy-storchaka

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.16new features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions