我的问题是,我想能够在Python控制台中覆盖/清除先前打印的行。这个问题已经被问了很多次(例如Python - Remove and Replace Printed items),但是用于解决此问题的代码(对我来说被标记为正确的答案)根本没有打印任何内容:
for i in range(10):
print("Loading" + "." * i)
time.sleep(1)
sys.stdout.write("\033[F") # Cursor up one line
sys.stdout.write("\033[K") # Clear to the end of line
我在Python IDLE中获得了输出:
Loading
[F[KLoading.
[F[KLoading..
[F[KLoading...
[F[KLoading....
[F[KLoading.....
[F[KLoading......
[F[KLoading.......
[F[KLoading........
[F[KLoading.........
[F[KLoading..........
[F[K
有什么想法吗?我在谷歌上搜了很多,但真的没什么用。它要么什么都不打印,要么就是不能覆盖。
如果有帮助的话,我正在运行Windows 8.1和Python 3.51。通过cmd运行代码并不会影响任何东西。
此外,添加sys.stdout.flush()
也没有帮助。
\033[F
的序列 (参见termcap(3)
)。据我所知,Windows 对终端功能的支持一直非常差。 - Andrea Corbellini