Skip to content

csv.Sniffer misdetects skipinitialspace #154744

Closed

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

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    3.16new features, bugs and security fixes
    on Jul 26, 2026
  2. added a commit that references this issue on Jul 27, 2026
  3. added a commit that references this issue on Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions