Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
31 lines (23 sloc) 722 Bytes
import csv
import re
from collections import namedtuple
def generate_rows(f):
"""
Generator that builds Row namedtuple from first row, then
yields Row's from subsequent rows
"""
spaces = re.compile(r'\s')
headers = tuple(re.sub(spaces, '', i) for i in next(f, tuple()))
Row = namedtuple('Row', headers)
for line in f:
yield Row(*(i.strip() for i in line))
def generate_csv_rows(csv_path):
"""
Wraps generate_rows with csv open, reader machinery. i.e.:
for line in generate_csv_rows('data.csv'):
print line
"""
with open(csv_path, 'rU') as f:
reader = csv.reader(f)
for line in generate_rows(reader):
yield line