Skip to content

Instantly share code, notes, and snippets.

@haard
Last active November 27, 2016 22:48
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haard/5295600 to your computer and use it in GitHub Desktop.
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/
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