Project

General

Profile

« Previous | Next » 

Revision 5571

csvs.py: Added InputRewriter, which wraps a reader, writing each row back to CSV

View differences:

lib/csvs.py
144 144
    def __init__(self, reader, cols_ct):
145 145
        def filter_(row): return util.list_as_length(row, cols_ct)
146 146
        Filter.__init__(self, filter_, reader)
147

  
148
##### Translators
149

  
150
class InputRewriter:
151
    '''Wraps a reader, writing each row back to CSV'''
152
    def __init__(self, reader, dialect=csv.excel):
153
        self.reader = reader
154
        self.dialect = dialect
155
    
156
    def readline(self):
157
        try: row = self.reader.next()
158
        except StopIteration: return '' # EOF
159
        line_stream = StringIO.StringIO()
160
        csv.writer(line_stream, self.dialect).writerow(row)
161
        return line_stream.getvalue()
162
    
163
    def read(self, n): return self.readline() # forward all reads to readline()

Also available in: Unified diff