1
|
# CSV I/O
|
2
|
|
3
|
import csv
|
4
|
import StringIO
|
5
|
|
6
|
delimiters = ',\t`'
|
7
|
|
8
|
def reader_and_header(stream):
|
9
|
'''Automatically detects the dialect based on the header line
|
10
|
@return tuple (reader, header)'''
|
11
|
header_line = stream.readline()
|
12
|
sniffer = csv.Sniffer()
|
13
|
dialect = sniffer.sniff(header_line, delimiters)
|
14
|
dialect.doublequote = True # Sniffer doesn't turn this on by default
|
15
|
header = csv.reader(StringIO.StringIO(header_line), dialect).next()
|
16
|
return (csv.reader(stream, dialect), header)
|