Skip to content

Instantly share code, notes, and snippets.

@pvarsh
Created November 20, 2015 01:25
Show Gist options
  • Save pvarsh/71d6f10eef8db3fa0335 to your computer and use it in GitHub Desktop.
Save pvarsh/71d6f10eef8db3fa0335 to your computer and use it in GitHub Desktop.
Decorator: csv reader
import csv
import string
def make_csv():
with open('data.csv', 'wb') as fh:
writer = csv.writer(fh)
for i in range(10):
writer.writerow([i, string.lowercase[i:i+5]])
def process_line(line):
return [int(line[0]), line[1].title()]
def csv_wrapper(func):
def opener(line_transform):
with open('data.csv', 'rb') as fh:
reader = csv.reader(fh)
data = list(func(reader, line_transform))
return data
return opener
@csv_wrapper
def process_reader(reader, line_transform):
for line in reader:
yield line_transform(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment