Revision 2114
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/csvs.py | ||
---|---|---|
79 | 79 |
# Note that these methods only work on *instances* of Dialect classes |
80 | 80 |
csv.Dialect.__eq__ = lambda self, other: self.__dict__ == other.__dict__ |
81 | 81 |
csv.Dialect.__ne__ = lambda self, other: not (self == other) |
82 |
|
|
83 |
##### Row filters |
|
84 |
|
|
85 |
class Filter: |
|
86 |
'''Wraps a reader, filtering each row''' |
|
87 |
def __init__(self, filter_, reader): |
|
88 |
self.reader = reader |
|
89 |
self.filter = filter_ |
|
90 |
|
|
91 |
def __iter__(self): return self |
|
92 |
|
|
93 |
def next(self): return self.filter(self.reader.next()) |
|
94 |
|
|
95 |
std_nulls = [r'\N'] |
|
96 |
empty_nulls = [''] + std_nulls |
|
97 |
|
|
98 |
class NullFilter(Filter): |
|
99 |
'''Translates special string values to None''' |
|
100 |
def __init__(self, reader, nulls=std_nulls): |
|
101 |
map_ = dict.fromkeys(nulls, None) |
|
102 |
def filter_(row): return [map_.get(v, v) for v in row] |
|
103 |
Filter.__init__(self, filter_, reader) |
|
104 |
|
|
105 |
class StripFilter(Filter): |
|
106 |
'''Strips whitespace''' |
|
107 |
def __init__(self, reader): |
|
108 |
def filter_(row): return [v.strip() for v in row] |
|
109 |
Filter.__init__(self, filter_, reader) |
Also available in: Unified diff
csvs.py: Added row filters