CSV sniffing detects wrong field delimiter

Open
#97,611 7 comments 0 reactions 1 assignee View on GitHub

Nobody has claimed this yet.

Assessment

This issue has not been assessed yet.

Description

3.13 3.14 3.15 stdlib type-bug

Bug report

csv.Sniffer().sniff() detects the wrong field delimiter if the possible valid delimiters contain \t and the provided data contains one line starting with the combination of double quotes followed by a tab and has the same combination of double quotes and a tab at least once more afterwards in the data. In that case the delimiter is always reported as \t instead of the correct one.

Configuring the preferred separator as suggested in https://github.com/python/cpython/issues/80678#issuecomment-1093819685 doesn't have any effect on this behavior.

Here is an example to reproduce it:

import csv
from pprint import pprint

data = """
Surname;First Name;Year of birth
"\tDoe;Jane"\t;1971
"""

sniffer = csv.Sniffer()
dialect = sniffer.sniff(data, delimiters=",;\t")
pprint(dict(dialect.__dict__))

Expected output:

{'__doc__': None,
 '__module__': 'csv',
 '_name': 'sniffed',
 'delimiter': ';',
 'doublequote': False,
 'lineterminator': '\r\n',
 'quotechar': '"',
 'quoting': 0,
 'skipinitialspace': False}

Actual output:

{'__doc__': None,
 '__module__': 'csv',
 '_name': 'sniffed',
 'delimiter': '\t',
 'doublequote': False,
 'lineterminator': '\r\n',
 'quotechar': '"',
 'quoting': 0,
 'skipinitialspace': False}

Your environment

  • CPython versions tested on: 3.7.14, 3.8.14, 3.9.14, 3.10.7
  • Operating system and architecture: Debian/sid on amd64
Dominant language
Python
Stars
77.2k
Forks
36k
Avg merge
1d 9h
Merged PRs (30d)
558

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.

More from python/cpython

All issues in python/cpython

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.