Created
December 8, 2016 21:17
-
-
Save fperucic/bb50bae0a0f7713eb99e5884e8d63b75 to your computer and use it in GitHub Desktop.
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
# -*- coding: utf-8 -*- | |
from collections import OrderedDict | |
import csv | |
def nl(data): | |
""" remove new lines """ | |
return data.replace('\r', '').replace('\n', '') | |
class CsvFileReader(object): | |
class Row(object): | |
def __init__(self, data, _columns): | |
self.data = data | |
self._columns = _columns | |
def get(self, key, typ=None): | |
return nl(self.data[self._columns[key]]) | |
def __init__(self, filename): | |
self.filename = filename | |
def __iter__(self): | |
_columns = OrderedDict() | |
with open(self.filename, 'r') as data: | |
spamreader = csv.reader(data, delimiter=',') | |
for i, row in enumerate(spamreader): | |
if i == 0: | |
for j, label in enumerate(row): | |
_columns.update({nl(label): j}) | |
else: | |
r = self.Row(row, _columns) | |
if any(r.data): | |
yield r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment