Created
November 20, 2015 01:25
-
-
Save pvarsh/71d6f10eef8db3fa0335 to your computer and use it in GitHub Desktop.
Decorator: 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 | |
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