Create a gist now

Instantly share code, notes, and snippets.

@haard /battery.py
Last active Nov 27, 2016

What would you like to do?
Batteries included: Download, unzip and parse in 13 lines - http://blaag.haard.se/Batteries-included--Download--unzip-and-parse-in-13-lines/
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