Skip to content

Instantly share code, notes, and snippets.

@vpetro
Created October 17, 2012 17:46
Show Gist options
  • Save vpetro/3906995 to your computer and use it in GitHub Desktop.
Save vpetro/3906995 to your computer and use it in GitHub Desktop.
slightly more useful eager csv reader
import csv
class csvreader(object):
def __init__(self, filepath):
self._filename = filepath
reader = csv.DictReader(
open(self._filename, 'rU')
)
self.fieldnames = reader.fieldnames
self._rows = [
row for row in reader
]
self._rowcount = len(self._rows)
def find(self, col_name, to_match, use_re=False):
assert col_name in self._fieldnames
for row in self._rows:
if to_match in row[col_name]:
return row
if use_re:
cur_value = row[col_name]
if re.findall(to_match, cur_value):
return row
return None
@property
def rows(self):
return self._rows
@property
def rowcount(self):
return self._rowcount
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment