How do I read selected files from a remote Zip archive over HTTP using Python?
Question: I need to read selected files, matching on the file name, from a remote zip
archive using Python. I don't want to save the full zip to a temporary file
(it's not that large, so I can handle everything in memory).

I've already written the code and it works, and I'm answering this myself so I
can search for it later. But since evidence suggests that I'm one of the
dumber participants on Stackoverflow, I'm sure there's room for improvement.


Answer: Here's how I did it (grabbing all files ending in ".ranks"):

    
    
    import urllib2, cStringIO, zipfile
    
    try:
        remotezip = urllib2.urlopen(url)
        zipinmemory = cStringIO.StringIO(remotezip.read())
        zip = zipfile.ZipFile(zipinmemory)
        for fn in zip.namelist():
            if fn.endswith(".ranks"):
                ranks_data = zip.read(fn)
                for line in ranks_data.split("\n"):
                    # do something with each line
    except urllib2.HTTPError:
        # handle exception
    

