gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 02:56:07 27 4
gpt4 key购买 nike

我可以找到一些 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

关于python - 在线下载数据时如何使用python中的 `tqdm`显示进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40544123/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com