Created
October 17, 2012 17:46
-
-
Save vpetro/3906995 to your computer and use it in GitHub Desktop.
slightly more useful eager csv reader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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