1 |
1388
|
aaronmk
|
# CSV I/O
|
2 |
|
|
|
3 |
|
|
import csv
|
4 |
5431
|
aaronmk
|
import _csv
|
5 |
1388
|
aaronmk
|
import StringIO
|
6 |
|
|
|
7 |
1623
|
aaronmk
|
import strings
|
8 |
1444
|
aaronmk
|
import util
|
9 |
|
|
|
10 |
5426
|
aaronmk
|
delims = ',;\t|`'
|
11 |
1623
|
aaronmk
|
tsv_delim = '\t'
|
12 |
|
|
escape = '\\'
|
13 |
1442
|
aaronmk
|
|
14 |
1623
|
aaronmk
|
ending_placeholder = r'\n'
|
15 |
|
|
|
16 |
5437
|
aaronmk
|
def is_tsv(dialect): return dialect.delimiter.startswith(tsv_delim)
|
17 |
1623
|
aaronmk
|
|
18 |
1621
|
aaronmk
|
def sniff(line):
|
19 |
|
|
'''Automatically detects the dialect'''
|
20 |
5435
|
aaronmk
|
line, ending = strings.extract_line_ending(line)
|
21 |
1623
|
aaronmk
|
dialect = csv.Sniffer().sniff(line, delims)
|
22 |
5435
|
aaronmk
|
|
23 |
1621
|
aaronmk
|
# TSVs usually don't quote fields (nor doublequote embedded quotes)
|
24 |
1623
|
aaronmk
|
if is_tsv(dialect): dialect.quoting = csv.QUOTE_NONE
|
25 |
1621
|
aaronmk
|
else: dialect.doublequote = True # Sniffer doesn't turn this on by default
|
26 |
5435
|
aaronmk
|
dialect.lineterminator = ending
|
27 |
|
|
|
28 |
1621
|
aaronmk
|
return dialect
|
29 |
|
|
|
30 |
1923
|
aaronmk
|
def stream_info(stream, parse_header=False):
|
31 |
1444
|
aaronmk
|
'''Automatically detects the dialect based on the header line
|
32 |
1923
|
aaronmk
|
@return NamedTuple {header_line, header, dialect}'''
|
33 |
1444
|
aaronmk
|
info = util.NamedTuple()
|
34 |
|
|
info.header_line = stream.readline()
|
35 |
1923
|
aaronmk
|
info.header = None
|
36 |
|
|
if info.header_line != '':
|
37 |
|
|
info.dialect = sniff(info.header_line)
|
38 |
|
|
if parse_header:
|
39 |
|
|
info.header = reader_class(info.dialect)(
|
40 |
|
|
StringIO.StringIO(info.header_line), info.dialect).next()
|
41 |
1660
|
aaronmk
|
else: info.dialect = None # line of '' indicates EOF = empty stream
|
42 |
1444
|
aaronmk
|
return info
|
43 |
|
|
|
44 |
5170
|
aaronmk
|
tsv_encode_map = strings.json_encode_map[:]
|
45 |
|
|
tsv_encode_map.append(('\t', r'\t'))
|
46 |
|
|
tsv_decode_map = strings.flip_map(tsv_encode_map)
|
47 |
5146
|
aaronmk
|
|
48 |
1623
|
aaronmk
|
class TsvReader:
|
49 |
|
|
'''Unlike csv.reader, for TSVs, interprets \ as escaping a line ending but
|
50 |
5145
|
aaronmk
|
ignores it before everything else (e.g. \N for NULL).
|
51 |
5170
|
aaronmk
|
Also expands tsv_encode_map escapes.
|
52 |
5145
|
aaronmk
|
'''
|
53 |
1623
|
aaronmk
|
def __init__(self, stream, dialect):
|
54 |
|
|
assert is_tsv(dialect)
|
55 |
|
|
self.stream = stream
|
56 |
|
|
self.dialect = dialect
|
57 |
|
|
|
58 |
|
|
def __iter__(self): return self
|
59 |
|
|
|
60 |
|
|
def next(self):
|
61 |
|
|
record = ''
|
62 |
|
|
ending = None
|
63 |
|
|
while True:
|
64 |
|
|
line = self.stream.readline()
|
65 |
|
|
if line == '': raise StopIteration
|
66 |
|
|
|
67 |
5438
|
aaronmk
|
line = strings.remove_suffix(self.dialect.lineterminator, line)
|
68 |
5433
|
aaronmk
|
contents = strings.remove_suffix(escape, line)
|
69 |
1623
|
aaronmk
|
record += contents
|
70 |
5433
|
aaronmk
|
if len(contents) == len(line): break # no line continuation
|
71 |
1623
|
aaronmk
|
record += ending_placeholder
|
72 |
3055
|
aaronmk
|
|
73 |
|
|
# Prevent "new-line character seen in unquoted field" errors
|
74 |
|
|
record = record.replace('\r', ending_placeholder)
|
75 |
|
|
|
76 |
5429
|
aaronmk
|
row = record.split(self.dialect.delimiter)
|
77 |
5170
|
aaronmk
|
return [strings.replace_all(tsv_decode_map, v) for v in row]
|
78 |
1623
|
aaronmk
|
|
79 |
|
|
def reader_class(dialect):
|
80 |
|
|
if is_tsv(dialect): return TsvReader
|
81 |
|
|
else: return csv.reader
|
82 |
|
|
|
83 |
|
|
def make_reader(stream, dialect): return reader_class(dialect)(stream, dialect)
|
84 |
|
|
|
85 |
1388
|
aaronmk
|
def reader_and_header(stream):
|
86 |
|
|
'''Automatically detects the dialect based on the header line
|
87 |
|
|
@return tuple (reader, header)'''
|
88 |
1923
|
aaronmk
|
info = stream_info(stream, parse_header=True)
|
89 |
1958
|
aaronmk
|
return (make_reader(stream, info.dialect), info.header)
|
90 |
1446
|
aaronmk
|
|
91 |
|
|
##### csv modifications
|
92 |
|
|
|
93 |
|
|
# Note that these methods only work on *instances* of Dialect classes
|
94 |
|
|
csv.Dialect.__eq__ = lambda self, other: self.__dict__ == other.__dict__
|
95 |
|
|
csv.Dialect.__ne__ = lambda self, other: not (self == other)
|
96 |
2114
|
aaronmk
|
|
97 |
5430
|
aaronmk
|
__Dialect__validate_orig = csv.Dialect._validate
|
98 |
|
|
def __Dialect__validate(self):
|
99 |
|
|
try: __Dialect__validate_orig(self)
|
100 |
|
|
except _csv.Error, e:
|
101 |
|
|
if str(e) == '"delimiter" must be an 1-character string': pass # OK
|
102 |
|
|
else: raise
|
103 |
|
|
csv.Dialect._validate = __Dialect__validate
|
104 |
|
|
|
105 |
2114
|
aaronmk
|
##### Row filters
|
106 |
|
|
|
107 |
|
|
class Filter:
|
108 |
|
|
'''Wraps a reader, filtering each row'''
|
109 |
|
|
def __init__(self, filter_, reader):
|
110 |
|
|
self.reader = reader
|
111 |
|
|
self.filter = filter_
|
112 |
|
|
|
113 |
|
|
def __iter__(self): return self
|
114 |
|
|
|
115 |
|
|
def next(self): return self.filter(self.reader.next())
|
116 |
|
|
|
117 |
|
|
std_nulls = [r'\N']
|
118 |
|
|
empty_nulls = [''] + std_nulls
|
119 |
|
|
|
120 |
|
|
class NullFilter(Filter):
|
121 |
|
|
'''Translates special string values to None'''
|
122 |
|
|
def __init__(self, reader, nulls=std_nulls):
|
123 |
|
|
map_ = dict.fromkeys(nulls, None)
|
124 |
|
|
def filter_(row): return [map_.get(v, v) for v in row]
|
125 |
|
|
Filter.__init__(self, filter_, reader)
|
126 |
|
|
|
127 |
|
|
class StripFilter(Filter):
|
128 |
|
|
'''Strips whitespace'''
|
129 |
|
|
def __init__(self, reader):
|
130 |
|
|
def filter_(row): return [v.strip() for v in row]
|
131 |
|
|
Filter.__init__(self, filter_, reader)
|