问题描述
要对参数空间进行大范围的扫描以进行科学计算,这需要花费我计算机上几个小时的时间,所以我想实现一个进度条(例如来自tqdm
包的进度条)。在程序的计算过程中,我希望程序在其搜索的网格上精确打印位置,并使用print()
打印一些错误消息。使用tqdm
中的标准代码会在每几行代码中显示一个条,但不会在终端的最后一行中显示进度条,因为这对我的应用程序很有益。
这是我的意思的最低版本:
from tqdm import tqdm
from time import sleep
for i in tqdm(range(10),position=0,leave=True):
print("\nAfter this comment there will be a new progress bar.")
sleep(0.5)
0%| | 0/10 [00:00<?,?it/s]
After this comment there will be a new progress bar.
10%|████▍ | 1/10 [00:00<00:04,2.00it/s]
After this comment there will be a new progress bar.
20%|████████▊ | 2/10 [00:01<00:04,2.00it/s]
After this comment there will be a new progress bar.
30%|█████████████▏ | 3/10 [00:01<00:03,2.00it/s]
After this comment there will be a new progress bar.
40%|█████████████████▌ | 4/10 [00:02<00:03,1.99it/s]
After this comment there will be a new progress bar.
50%|██████████████████████ | 5/10 [00:02<00:02,1.99it/s]
After this comment there will be a new progress bar.
60%|██████████████████████████▍ | 6/10 [00:03<00:02,1.99it/s]
After this comment there will be a new progress bar.
70%|██████████████████████████████▊ | 7/10 [00:03<00:01,1.99it/s]
After this comment there will be a new progress bar.
80%|███████████████████████████████████▏ | 8/10 [00:04<00:01,1.99it/s]
After this comment there will be a new progress bar.
90%|███████████████████████████████████████▌ | 9/10 [00:04<00:00,1.99it/s]
After this comment there will be a new progress bar.
100%|███████████████████████████████████████████| 10/10 [00:05<00:00,1.99it/s]
您是否知道tqdm的替代方法,快速实现的进度条仍在下面并且会不断增长(以及tqdm提供的功能)还是也许我只需要给出tqdm正确的论点?
谢谢!
解决方法
听起来您正在寻找tqdm的.write()方法。
也就是说,如果您确实需要文本输出和进度条,那么我的解决方案是登录到文件。
请注意,使用默认值,您可以知道打印错误/输出的迭代次数,而不必通过变量/ exc_info或类似变量来提供。因此,我很少使用tqdm.write()。