python / python/cpython

Add a delimiters parameter to the csv.Sniffer constructor

Open
#155,059 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.16 stdlib type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Feature or enhancement

csv.Sniffer.sniff() accepts an optional delimiters argument which restricts the set of characters that can be guessed as a delimiter. has_header() has no such argument, so the restriction cannot be applied to it.

I propose to add an optional delimiters parameter to the constructor, so that it applies to the sniffer rather than to a single call:

sniffer = csv.Sniffer(',;\t')
if sniffer.has_header(sample):
    ...
dialect = sniffer.sniff(sample)

It is used by both sniff() and has_header(). The delimiters argument of sniff(), if given, still takes precedence.

The given delimiters are also preferred in that order if several of them split the sample equally well: csv.Sniffer(';,') guesses ';' where csv.Sniffer(',;') guesses ','.

Linked PRs
  • gh-155060

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the csv.Sniffer constructor, sniff(), and has_header() entry points described in the issue, then inspect the existing CSV tests for their current behavior. Done means the constructor's delimiters restriction is used by both methods, sniff() can override it, and equal candidates follow the supplied delimiter order.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.