Skip to content

Instantly share code, notes, and snippets.

@dokterbob
Created November 10, 2014 18:43
Show Gist options
  • Save dokterbob/94a212e138c53653e957 to your computer and use it in GitHub Desktop.
Save dokterbob/94a212e138c53653e957 to your computer and use it in GitHub Desktop.
Python dialect-sniffing CSV reader example.
import csv
sniff_range = 4096
delimiters = ';\t,'
infile_name = 'blabla.csv'
sniffer = csv.Sniffer()
print 'Reading file: %s' % infile_name
with open(infile_name, 'r') as infile:
# Determine dialect
dialect = sniffer.sniff(
infile.read(sniff_range), delimiters=delimiters
)
infile.seek(0)
# Sniff for header
has_header = sniffer.has_header(infile.read(sniff_range))
infile.seek(0)
reader = csv.reader(infile, dialect)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment