On this page
csv.Sniffer
Prev: csv.register_dialect | Up: csv | Next: csv.unix_dialect
Rel: csv.Dialect, csv.DictReader, csv.DictWriter, csv.Error
Ref: docs.python.org — csv.Sniffer
Tags: #public #python3 #stdlib #csv
Import
import csv
from csv import Sniffer
Signature & Overview
csv.Sniffer()
"Sniffs" the format of a CSV file (i.e. delimiter, quotechar) Returns a Dialect object.
Methods & Attributes
Sniffer.has_header(self, sample)—Sniffer.has_headermethod/property.Sniffer.sniff(self, sample, delimiters=None)— Returns a dialect (or None) corresponding to the sample If several delimiters fit the sample equally well, the delimiters listed in the preferred attribute are preferred, in
Usage
import csv
sample = "name;age;city\nAda;36;London\nGrace;85;New York\n"
sniffer = csv.Sniffer()
dialect = sniffer.sniff(sample)
assert dialect.delimiter == ";"
assert sniffer.has_header(sample) is True
Gotchas
- Heuristic, not infallible:
Sniffercan misguess on tiny samples or single-column files—passdelimiters=",;\t|"to constrain candidates and catchcsv.Error.
REPL Inspect
from csv import Sniffer
help(Sniffer)
Linked from
Related notes
- tuts/python3/stdlib_module/csv/register_dialect Linked here; links to this page; shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv/unix_dialect Linked here; links to this page; shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/csv/Dialect Linked here; shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv/DictReader Linked here; shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv/DictWriter Linked here; shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv/Error Linked here; shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv/excel shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv/excel_tab shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv/field_size_limit shared tags: #csv, #stdlib