Revision 6589
Added by Aaron Marcuse-Kubitza about 12 years ago
lib/csvs.py | ||
---|---|---|
38 | 38 |
return dialect |
39 | 39 |
|
40 | 40 |
def stream_info(stream, parse_header=False): |
41 |
'''Automatically detects the dialect based on the header line |
|
41 |
'''Automatically detects the dialect based on the header line. |
|
42 |
Uses the Excel dialect if the CSV file is empty. |
|
42 | 43 |
@return NamedTuple {header_line, header, dialect}''' |
43 | 44 |
info = util.NamedTuple() |
44 | 45 |
info.header_line = stream.readline() |
45 | 46 |
info.header = None |
46 | 47 |
if info.header_line != '': |
47 | 48 |
info.dialect = sniff(info.header_line) |
48 |
if parse_header: |
|
49 |
info.header = reader_class(info.dialect)( |
|
50 |
StringIO.StringIO(info.header_line), info.dialect).next() |
|
51 |
else: info.dialect = None # line of '' indicates EOF = empty stream |
|
49 |
else: info.dialect = csv.excel # line of '' indicates EOF = empty stream |
|
50 |
|
|
51 |
if parse_header: |
|
52 |
try: info.header = reader_class(info.dialect)( |
|
53 |
StringIO.StringIO(info.header_line), info.dialect).next() |
|
54 |
except StopIteration: info.header = [] |
|
55 |
|
|
52 | 56 |
return info |
53 | 57 |
|
54 | 58 |
tsv_encode_map = strings.json_encode_map[:] |
Also available in: Unified diff
csvs.py: stream_info(): Use the Excel dialect and an empty header if the CSV file is empty