Last active
November 27, 2016 22:48
-
-
Save haard/5295600 to your computer and use it in GitHub Desktop.
Batteries included: Download, unzip and parse in 13 lines - http://blaag.haard.se/Batteries-included--Download--unzip-and-parse-in-13-lines/
This file contains hidden or 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 zipfile, urllib, csv, os, codecs | |
def get_items(url): | |
filename, headers = urllib.urlretrieve(url) | |
try: | |
with zipfile.ZipFile(filename) as zf: | |
csvfiles = [name for name in zf.namelist() | |
if name.endswith('.csv')] | |
for item in csvfiles: | |
with zf.open(item) as source: | |
reader = csv.DictReader(codecs.getreader('iso-8859-1')(source)) | |
for line in reader: | |
yield line | |
finally: | |
os.unlink(filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment