python - 进度条 Python
问题描述
我有以下代码:
def progressbar(count, total, status=""):
bar_len = 40
filled_len = int(round(bar_len * count / float(total)))
percents = round(100.1 * count / float(total), 1)
bar = "X" * filled_len + "-" * (bar_len - filled_len)
print("[{}] {}{} ...{}".format(bar, percents, "%", status),
end="\r", flush=True)
并调用进度条:
total = 100
i = 0
while i < total:
i += 1
progressbar(i, total, status="Creating stuff")
time.sleep(1)
其中 total 是迭代次数。当我运行此代码时,我会在多行而不是仅一行上运行进度条。有什么建议吗?
解决方案
我认为您的代码没有任何问题。
print("[{}] {}{} ...{}".format(bar, percents, "%", status),
**end="\r"**, flush=True)
print() 方法中的 end="\n" 参数应将进度条打印一行。
如果您在终端中运行此代码,它应该可以正常工作。
我之前看到过一个 PyCharm IDE 的问题,其中 end="\r" 不起作用。这可能是 IDE 中终端仿真器中的错误。
推荐阅读
- javascript - 我有一组承诺,我如何“按顺序”解决每个组?
- angular - Angular 自定义库类型验证
- css - img 样式正在改变表情符号的显示方式
- java - Spring RabbitMQ:在没有自定义注释的情况下实现额外的验证
- java - Spring Batch JdbcPagingItemReader 将日期范围作为参数添加到查询
- javascript - 需要在函数中使用用户的输入
- javascript - 在 Angular 的 tinymce 编辑器中删除图片时无法从服务器中删除图片
- r - H2o.mojo_predict_df 给出退出状态 1
- jupyter-notebook - Jupyter Notebook 上的白线阻止内核运行单元格
- java - Java 11 获取当前的微秒?