python - 如何在Python中将光标重置到同一行的开头

标签 python

SO 中与此主题相关的大部分问题如下:

How to print some information on the same line without introducing a new line

Q1 Q2 .

相反,我的问题如下:

我希望看到以下效果,

>> You have finished 10%

其中10同时不断增加。我知道如何在 C++ 中做到这一点,但不能 在python中找到一个好的解决方案。

最佳答案

import sys, time

for i in xrange(0, 101, 10):
  print '\r>> You have finished %d%%' % i,
  sys.stdout.flush()
  time.sleep(2)
print

\r 是回车符。您需要在 print 语句末尾使用逗号以避免自动换行。最后需要 sys.stdout.flush() 将缓冲区刷新到标准输出。

对于 Python 3,您可以使用:

print("\r>> You have finished {}%".format(i), end='')

关于python - 如何在Python中将光标重置到同一行的开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7715594/

相关文章:

python - 频率和百分比不均匀组 sns barplot

Python 日志记录 : INFO, DEBUG 日志未显示

python - Docker 从守护进程获取事件时出错 : EOF

python - 在 Python 中使用季节性分解时我做错了什么?

python - for 循环中变量的值

python - 如何在 Python 中按顺序打印为类创建的所有变量的列表?

python - 使用Python计算坡度的程序

python - Argparse - 一次访问多个参数

Python、 `let`、 `with`、局部作用域、调试打印和临时变量

javascript - 每篇文章都有一个单独的链接(bootstrap modal、django、jQuery)