python-3.x - 如何在python3.7中固定的某个地方打印任务的进度
问题描述
我正在尝试运行一个耗时的“for”循环,并希望在控制台的固定位置打印该过程并刷新每个循环
我尝试这样的打印功能
for i in range(total):
...some code....
print('---%.2f---'%(float(i)/total), sep='')
好像不行
解决方案
我用这个答案来显示进度条:
使用的方法:
def startProgress(title):
global progress_x
sys.stdout.write(title + ": [" + "-"*50 + "]" + chr(8)*51)
sys.stdout.flush()
progress_x = 0
def progress(x):
global progress_x
x = int(x * 50 // 100)
sys.stdout.write("#" * (x - progress_x))
sys.stdout.flush()
progress_x = x
def endProgress():
sys.stdout.write("#" * (50 - progress_x) + "]\n")
sys.stdout.flush()
使用示例:
import time
import sys
startProgress("Test")
for i in range(500):
progress(i * 100 / 500)
time.sleep(.1)
endProgress()
#
随着进度的同时移动,进度将如下所示:
推荐阅读
- java - maven 无法解决对 google firebase 的依赖
- ios - 将数据从标签栏控制器传输到 ViewController
- git - 即使在将公共 ssh 密钥添加到 Git Hub 之后,也无法将更改从我的本地系统推送到源站
- python - Python Beatifulsoup 无法正确使用 selenium 的结果
- vba - 如何从今天收到的 Outlook 邮件中提取电子邮件附件?
- azure - 特定国家/地区用户应访问特定国家/地区的网站,但数据库应跨地区同步
- c# - 如何使用来自另一个分部类的分部类中的方法
- php - laravel和vue如何同时保存和更新
- python - 有没有办法从模型中获取并显示用户发布的每个广告的图像数量
- networking - 端口 80 的端口转发不适用于来自 LAN 内部的请求(适用于 WAN)