简体   繁体   English

在OS X上的python中,如何打印光标并将光标留在同一行的开头?

[英]In python on OS X, how can I print and leave the cursor at the beginning of the same line?

I would like my program to display some status messages as it runs, each one overwriting the previous. 我希望程序在运行时显示一些状态消息,每个消息都覆盖前一个。 In Windows, I have been using the following to print a message and leave the cursor at the beginning of the line ready for the next message: 在Windows中,我一直在使用以下命令打印一条消息,并将光标留在行的开头以准备下一条消息:

print message, '\r',

On OS X, however, this does not work - the cursor is at the beginning of the next line. 但是,在OS X上,此操作不起作用-光标位于下一行的开头。 It was suggested that I change \\r to \\x1b[0G , but this has the same effect. 建议将\\r更改为\\x1b[0G ,但这具有相同的效果。

How can I leave the cursor at the beginning of the current line? 如何将光标留在当前行的开头?

This seems to do it in a Terminal window: 这似乎是在终端窗口中完成的:

import time
import sys

print 'Hello\r',
sys.stdout.flush()
time.sleep(3)
print 'There\r',
sys.stdout.flush()
time.sleep(3)

Sorry for the inconvenience - turns out \\r works fine. 很抱歉给您带来不便-事实证明\\r效果很好。 The issue was caused by message being longer than a single line. 此问题是由于message的长度超过一行。

This never happened on Windows because I have a Screen Buffer Width far in excess of my Window Width , whereas on OS X it seems the two are always the same. 这在Windows上从来没有发生过,因为我的Screen Buffer Width远远超过了我的Window Width ,而在OS X上似乎两者总是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM