X Tutup
The Wayback Machine - https://web.archive.org/web/20251210232504/https://github.com/python/cpython/issues/104380
Skip to content

Improve detection in csv.Sniffer.has_header #104380

@mewu3

Description

@mewu3

Bug report

csv file:

sample,is_nc,fastq_1,fastq_2
5B7822-M1-U055G,False,data/5B7822-M1-U055G_S12_R1_001.fastq.gz,
5B7822-M1-U042G,False,data/5B7822-M1-U042G_S3_R1_001.fastq.gz,
5B7821-M1-U054G,False,data/5B7821-M1-U054G_S11_R1_001.fastq.gz,
5B7821-M1-U041G,False,data/5B7821-M1-U041G_S2_R1_001.fastq.gz,
5M26799-M1-U043G,False,data/5M26799-M1-U043G_S4_R1_001.fastq.gz,
5M26799-M1-U056G,False,data/5M26799-M1-U056G_S13_R1_001.fastq.gz,
5M26800-M1-U044G,False,data/5M26800-M1-U044G_S5_R1_001.fastq.gz,
5M26800-M1-U057G,False,data/5M26800-M1-U057G_S14_R1_001.fastq.gz,
5M26802-M1-U046G,False,data/5M26802-M1-U046G_S6_R1_001.fastq.gz,

code:

def read_head(handle, num_lines=2):
    """Read the specified number of lines from the current position in the file."""
    lines = []
    for idx, line in enumerate(handle):
        if idx == num_lines:
            break
        lines.append(line)
    return "".join(lines)


def sniff_format(handle):
    """
    Detect the tabular format.

    Args:
        handle (text file): A handle to a `text file`_ object. The read position is
        expected to be at the beginning (index 0).

    Returns:
        csv.Dialect: The detected tabular format.

    .. _text file:
        https://docs.python.org/3/glossary.html#term-text-file

    """
    peek = read_head(handle)
    handle.seek(0)
    sniffer = csv.Sniffer()
    if not sniffer.has_header(peek):
        logger.critical("The given sample sheet does not appear to contain a header.")
        sys.exit(1)
    dialect = sniffer.sniff(peek)
    return dialect

Your environment

  • CPython versions tested on: 3.8.3
  • Operating system and architecture: Ubuntu 20.04.3 LTS

Comments

This bug does not only occur with v3.8.3, it occurs also with version 3.11.3.

Linked PRs

Metadata

Metadata

Labels

stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    X Tutup