Skip to content

Instantly share code, notes, and snippets.

@brettvitaz
Created October 3, 2015 01:15
Show Gist options
  • Select an option

  • Save brettvitaz/7fa517cafd2f28c47713 to your computer and use it in GitHub Desktop.

Select an option

Save brettvitaz/7fa517cafd2f28c47713 to your computer and use it in GitHub Desktop.
Wrapper for csv reader to give len() capabilities. *** Doesn't work as expected. ***
class CsvReaderWrapper(object):
def __init__(self, csv_file, **kwargs):
self._csv_file = csv_file
self._reader = csv.reader(csv_file, **kwargs)
def __len__(self):
pos = self._csv_file.tell()
self._csv_file.seek(0)
items = 0
for items, _ in enumerate(self._reader):
pass
self._csv_file.seek(pos)
return items
def __iter__(self):
return self
def next(self):
return self._reader.next()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment