Skip to content

csv.Sniffer returns a dialect that drops empty space-delimited fields #161400

Open

Description

@fedonman

Bug description:

With a space as the delimiter, csv.Sniffer.sniff() returns skipinitialspace=True for a sample with an unquoted empty field when one other row is shorter, so the sniffed dialect loses the empty field:

import csv, io
data = ('name age city\n'
        'alice 30 paris\n'
        'bob  london\n'
        'carol 25 "new york"\n'
        'dave 40 berlin\n'
        'erin 35\n')
dialect = csv.Sniffer().sniff(data)
print(dialect.skipinitialspace, list(csv.reader(io.StringIO(data), dialect))[2])

On main:

True ['bob', 'london']

On 3.15.0:

False ['bob', '', 'london']

The shorter row can also be the last row of a cut-off sample, as in the documented sniff(csvfile.read(1024)): with the last row 'erin 35 rome\n', sniffing data[:-6] gives the same result. The docs say sniff() picks the combination "with the most consistent number of fields", which here is skipinitialspace=False. This came with gh-154744.

Expected: skipinitialspace=False, as in 3.15.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 5, 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

    stdlibStandard 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