Skip to content

Commit

Permalink
Added CSVStream object
Browse files Browse the repository at this point in the history
  • Loading branch information
kfsone committed Feb 15, 2015
1 parent 3f62d79 commit 12d4d75
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions transfers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from urllib.parse import urlencode
from urllib.request import Request, urlopen

import csv
import json
import math
import misc.progress as pbar
Expand Down Expand Up @@ -211,3 +212,39 @@ def get_json_data(url):

return json.loads(jsData.decode())


class CSVStream(object):
"""
Provides an iterator that fetches CSV data from a given URL
and presents it as an iterable of (columns, values).
Example:
stream = transfers.CSVStream("http://blah.com/foo.csv")
for cols, vals in stream:
print("{} = {}".format(cols[0], vals[0]))
"""

def __init__(self, url):
self.url = url
self.req = requests.get(self.url, stream=True)
self.lines = self.req.iter_lines()
self.columns = self.next_line().split(',')

def next_line(self):
""" Fetch the next line as a text string """
line = next(self.lines).decode()
return line

def __iter__(self):
"""
Iterate across data received as csv values.
Yields [column headings], [column values]
"""
csvin = csv.reader(
iter(self.next_line, 'END'),
delimiter=',', quotechar="'", doublequote=True
)
columns = self.columns
for values in csvin:
if values and len(values) == len(columns):
yield columns, values

0 comments on commit 12d4d75

Please sign in to comment.