[英]Carriage return without line break on windows
所以我有這個簡單的statusbar實現:
def status_update(current, top, label="Progress"):
workdone = current/top
print("\r{0:s}: [{1:30s}] {2:.1f}%".format(label,'#' * int(workdone * 30), workdone*100), end="", flush=True)
if workdone == 1:
print()
在Linux上按預期工作。
但是,在Windows(以我為例,為10)上, \\r
顯然為每個輸出創建了新行,而不是覆蓋前一行。
我該如何阻止呢? (最好以不破壞linux兼容性的方式。)
這可能不是最好的方法,但是可以。 只需使用\\ b。
def status_update(current, top, label="Progress"):
workdone = current/top
if not hasattr(status_update, "length"):
status_update.length = 0
str1="{0:s}: [{1:30s}] {2:.1f}%".format(label,'#' * int(workdone * 30), workdone*100)
print(('\b'*status_update.length)+str1, end="", flush=True)
status_update.length=len(str1)
if workdone == 1:
print()
在這里我回退了status_update上次調用中打印的字符數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.