-
-
Save cpkthompson/0264a00fa3de9d81b5b2431a42ab8562 to your computer and use it in GitHub Desktop.
Python dialect-sniffing CSV reader example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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