python - Python 脚本中 "for"循环的进度条

标签 python python-2.7

我正在尝试在我拥有的 python 脚本中使用进度条,因为我有一个 for 循环需要相当多的时间来处理。我已经看过这里的其他解释,但我仍然感到困惑。这是我的 for 循环在我的脚本中的样子:

for member in members:
    url = "http://api.wiki123.com/v1.11/member?id="+str(member) 
    header = {"Authorization": authorization_code}
    api_response = requests.get(url, headers=header)
    member_check = json.loads(api_response.text)
    member_status = member_check.get("response") 

我已经阅读了一些关于使用 progressbar 库的内容,但我的困惑在于我必须将代码放在哪里以支持相对于我在此处包含的 for 循环的进度条。

最佳答案

使用 tqdm :

from tqdm import tqdm

for member in tqdm(members):
    # current contents of your for loop

tqdm() 接受 members 并对其进行迭代,但每次它产生一个新成员时(在循环的每次迭代之间),它还会更新一个进度条在你的命令行上。这实际上与 Matthias 的解决方案(在每次循环迭代结束时打印内容)非常相似,但进度条更新逻辑很好地封装在 tqdm 中。

关于python - Python 脚本中 "for"循环的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43259717/

相关文章:

python - 使用二进制和连续变量标准化数据以进行机器学习

Python Mechanize - 如何在下拉菜单中提交未列出的值

python - 在 Python 中,将 "dict"与关键字或匿名词典一起使用?

python - python 'import' 可以扩展到 zip 以外的其他存档类型吗?

python-2.7 - Pygame 键被​​按下

python - 将 Pandas 数据框转换为包含索引、数据和列的列表列表

python - 列表列表及其索引中的最大值

python - 将嵌套数据存储实体转换为 Python 字典的最简单方法是什么?

python - 在 PyQt 中显示图像(以字节为单位)

python-2.7 - 透明的误差线,不影响标记