在线下载数据时,如何在python中使用`tqdm`来显示进度?

我可以找到一些doc来解释如何使用tqdm软件包,但是从中我无法弄清楚在在线下载数据时如何生成进度表.

以下是我从ResidentMario复制的用于下载数据的示例代码

def download_file(url, filename):
    """
    Helper method handling downloading large files from `url` to `filename`. Returns a pointer to `filename`.
    """
    r = requests.get(url, stream=True)
    with open(filename, 'wb') as f:
        for chunk in r.iter_content(chunk_size=1024): 
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)
    return filename


dat = download_file("https://data.cityofnewyork.us/api/views/h9gi-nx95/rows.csv?accessType=DOWNLOAD",
                    "NYPD Motor Vehicle Collisions.csv")

谁能告诉我如何在这里使用tqdm软件包来显示下载进度?

谢谢

解决方法:

到目前为止,我正在做类似的事情:

def download_file(url, filename):
    """
    Helper method handling downloading large files from `url` to `filename`. Returns a pointer to `filename`.
    """
    chunkSize = 1024
    r = requests.get(url, stream=True)
    with open(filename, 'wb') as f:
        pbar = tqdm( unit="B", total=int( r.headers['Content-Length'] ) )
        for chunk in r.iter_content(chunk_size=chunkSize): 
            if chunk: # filter out keep-alive new chunks
                pbar.update (len(chunk))
                f.write(chunk)
    return filename
上一篇:tqdm 任务进度条模块


下一篇:python – 在Jupyter笔记本中的tqdm