Revision 1923
Added by Aaron Marcuse-Kubitza almost 13 years ago
lib/csvs.py | ||
---|---|---|
22 | 22 |
else: dialect.doublequote = True # Sniffer doesn't turn this on by default |
23 | 23 |
return dialect |
24 | 24 |
|
25 |
def stream_info(stream): |
|
25 |
def stream_info(stream, parse_header=False):
|
|
26 | 26 |
'''Automatically detects the dialect based on the header line |
27 |
@return NamedTuple {header_line, dialect}''' |
|
27 |
@return NamedTuple {header_line, header, dialect}'''
|
|
28 | 28 |
info = util.NamedTuple() |
29 | 29 |
info.header_line = stream.readline() |
30 |
if info.header_line != '': info.dialect = sniff(info.header_line) |
|
30 |
info.header = None |
|
31 |
if info.header_line != '': |
|
32 |
info.dialect = sniff(info.header_line) |
|
33 |
if parse_header: |
|
34 |
info.header = reader_class(info.dialect)( |
|
35 |
StringIO.StringIO(info.header_line), info.dialect).next() |
|
31 | 36 |
else: info.dialect = None # line of '' indicates EOF = empty stream |
32 | 37 |
return info |
33 | 38 |
|
... | ... | |
66 | 71 |
def reader_and_header(stream): |
67 | 72 |
'''Automatically detects the dialect based on the header line |
68 | 73 |
@return tuple (reader, header)''' |
69 |
info = stream_info(stream) |
|
70 |
reader_class_ = reader_class(info.dialect) |
|
71 |
header = reader_class_(StringIO.StringIO(info.header_line), |
|
72 |
info.dialect).next() |
|
73 |
return (reader_class_(stream, info.dialect), header) |
|
74 |
info = stream_info(stream, parse_header=True) |
|
75 |
return (reader_class(info.dialect)(stream, info.dialect), info.header) |
|
74 | 76 |
|
75 | 77 |
##### csv modifications |
76 | 78 |
|
Also available in: Unified diff
csvs.py: stream_info(): Added parse_header option. reader_and_header(): Use stream_info()'s new parse_header option.