Downloading and unzipping a .zip file without writing to disk

前端 未结 9 1524
囚心锁ツ
囚心锁ツ 2020-12-02 04:42

I have managed to get my first python script to work which downloads a list of .ZIP files from a URL and then proceeds to extract the ZIP files and writes them to disk.

9条回答
  •  有刺的猬
    2020-12-02 05:40

    Below is a code snippet I used to fetch zipped csv file, please have a look:

    Python 2:

    from StringIO import StringIO
    from zipfile import ZipFile
    from urllib import urlopen
    
    resp = urlopen("http://www.test.com/file.zip")
    zipfile = ZipFile(StringIO(resp.read()))
    for line in zipfile.open(file).readlines():
        print line
    

    Python 3:

    from io import BytesIO
    from zipfile import ZipFile
    from urllib.request import urlopen
    # or: requests.get(url).content
    
    resp = urlopen("http://www.test.com/file.zip")
    zipfile = ZipFile(BytesIO(resp.read()))
    for line in zipfile.open(file).readlines():
        print(line.decode('utf-8'))
    

    Here file is a string. To get the actual string that you want to pass, you can use zipfile.namelist(). For instance,

    resp = urlopen('http://mlg.ucd.ie/files/datasets/bbc.zip')
    zipfile = ZipFile(BytesIO(resp.read()))
    zipfile.namelist()
    # ['bbc.classes', 'bbc.docs', 'bbc.mtx', 'bbc.terms']
    

提交回复
热议问题