【问题标题】:How can i download a zipped file from the internet using pandas 0.17.1 and python 3.5如何使用 pandas 0.17.1 和 python 3.5 从 Internet 下载压缩文件
【发布时间】:2016-06-03 01:32:30
【问题描述】:

我做错了什么?这是我想要做的:

import pandas as pd

url='http://data.octo.dc.gov/feeds/crime_incidents/archive/crime_incidents_2013_CSV.zip'

df = pd.read_csv(url, compression='gzip',
                 header=0, sep=',', quotechar='"',
                 engine = 'python')

【问题讨论】:

  • 你遇到了什么问题?

标签: pandas download zip


【解决方案1】:

@Abbas,非常感谢。事实上,我一步一步地运行它,这就是我想出的。确实不是最快的,但效果很好。
我在 Mac 上的 python 3.5.1 上使用 pandas 0.18.1 运行它

from zipfile import ZipFile
from urllib.request import urlopen   
import pandas as pd
import os

URL = \
    'http://data.octo.dc.gov/feeds/crime_incidents/archive/crime_incidents_2013_CSV.zip'

# open and save the zip file onto computer
url = urlopen(URL)
output = open('zipFile.zip', 'wb')    # note the flag:  "wb"        
output.write(url.read())
output.close()

# read the zip file as a pandas dataframe
df = pd.read_csv('zipFile.zip')   # pandas version 0.18.1 takes zip files       

# if keeping on disk the zip file is not wanted, then:
os.remove(zipName)   # remove the copy of the zipfile on disk

我希望这会有所帮助。谢谢!

【讨论】:

    【解决方案2】:

    Cy Bu 的答案在 Windows 上的 Python 3.6 中不太适合我。尝试打开文件时出现无效参数错误。我稍微修改了一下:

    import os
    from urllib.request import urlopen, Request
    
    r = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
    b2 = [z for z in url.split('/') if '.zip' in z][0] #gets just the '.zip' part of the url
    
    with open(b2, "wb") as target:
        target.write(urlopen(r).read()) #saves to file to disk
    
    data = pd.read_csv(b2, compression='zip') #opens the saved zip file
    os.remove(b2) #removes the zip file
    

    【讨论】:

    • 这个答案对我来说是唯一可行的解​​决方案!稍微更新了它,因为如果您尝试以编程方式访问 API,通常会收到 403(禁止)错误。然后,您必须在对 API 的请求中指定“用户代理”。
    【解决方案3】:

    这里的IIUC是一个解决方案,而不是直接将zip文件传递给pandas,先解压然后传递csv文件:

    from StringIO import StringIO
    from zipfile import ZipFile
    from urllib import urlopen
    import pandas as pd
    
    url = urlopen("http://data.octo.dc.gov/feeds/crime_incidents/archive/crime_incidents_2013_CSV.zip")
    zipfile = ZipFile(StringIO(url.read()))
    f = open(zipfile.NameToInfo.keys()[0],'wb')
    f.write(zipfile.open(zipfile.NameToInfo.keys()[0]).read())
    f.close()
    
    df = pd.read_csv(zipfile.NameToInfo.keys()[0])
    

    并且会产生一个像这样的DataFrame

    【讨论】:

    • 此解决方案不起作用,因为我似乎无法使用 from urllib import url open... 我遇到的问题是我的代码引发了错误消息:文件"/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/zipfile.py",第 1093 行,在 _RealGetContents raise BadZipFile("File is not a zip file") zipfile.BadZipFile: File is not a压缩文件
    • 您是否能够使用浏览器下载 zip 文件?
    • 你能解压它(使用 7zip 或类似的应用程序)并将 csv 文件加载到pandas吗?
    • 是的,我可以手动下载任何 zip 文件并解压缩(在 Mac 上),没有任何问题。就像我可以在上面示例中给出的那个文件上做的那样。
    • 好的,如果是这样,我没有 Mac 来评估这个解决方案,理想情况下它应该可以工作。如果没有,那么一步一步地评估你在每一步之后得到的结果。就像只是将 url.read() 的内容写入文件并对其进行评估,然后在每一步中进行评估。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 2010-09-06
    相关资源
    最近更新 更多