\r(回车)如何在 Python 中工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18692617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how does \r (carriage return) work in Python
提问by user571099
OS: Windows 7
操作系统:Windows 7
my understanding is that \r moves the text to the left side of the page.
我的理解是 \r 将文本移动到页面的左侧。
however when I executed this:
但是,当我执行此操作时:
carriage_return = "I will use a carriage\rreturn"
print carriage_return
I got: return use a carriage
我得到:返回使用马车
what I was expecting was: return
我期待的是:返回
采纳答案by Snakes and Coffee
Well, it appears that you did move to the left of the line. It just left the rest of the line untouched. Note that return
is 6 chars, and I will
is also 6.
好吧,看来您确实移动到了行的左侧。它只是让该行的其余部分保持不变。请注意,这return
是 6 个字符,I will
也是 6。
回答by Seeker
\r takes the cursor to the beginning of the line. It is the same effect as in a physical typewriter when you move your carriage to the beginning and overwrite whatever is there.
\r 将光标移到行首。当您将笔架移到开头并覆盖那里的任何内容时,它的效果与在物理打字机中的效果相同。
回答by Shubham Meshram
All the characters after the \r escape sequence move to the left and overwrite exactly those number of characters present at the starting of the statement.
\r 转义序列之后的所有字符都向左移动并准确覆盖语句开头的那些字符数。
In your example, returnis there after \r and it is 5 characters and it replaces I willwhich is also 5 characters (don't forget to include space :P) so it basically replaces I willand prints return use a carriage
在您的示例中,return在 \r 之后,它是 5 个字符,它替换了也是 5 个字符的I will(不要忘记包含空格:P),因此它基本上替换了I will并使用回车符打印return
回答by Armstrongest
Just to add to this. If you want to return to the beginning and erase the contents of the line, you can do that like this:
只是为了补充这一点。如果您想返回到开头并删除该行的内容,您可以这样做:
text = "Here's a piece of text I want to overwrite"
repl = "BALEETED!" # What we want to write
print(text, end="\r") # Write the text and return
print(f'\r{repl: <{len(text)}}')
That last line might need a bit of explaining, so I'll break it down:
最后一行可能需要一些解释,所以我将分解它:
f'SOMETHING {var}'
is an f-string, equivalent to 'SOMETHING {}'.format('HERE')
. It's available in Python 3.6+
f'SOMETHING {var}'
是一个 f 字符串,相当于'SOMETHING {}'.format('HERE')
. 它在 Python 3.6+ 中可用
So replacing the hard-coded values for variables, we're returning to the beginning and then writing the replacement string, followed by enough spaces to replace the original text. If you want to use the old format method which is probably more clear in this case:
因此,替换变量的硬编码值,我们回到开头,然后编写替换字符串,后跟足够的空格来替换原始文本。如果您想使用在这种情况下可能更清楚的旧格式方法:
print('\r{0: <{1}}'.format(repl, len(text)))
# Which becomes when extrapolated:
print('BALEETED ')
For bonus points, you don't need to use spaces, you can use anything:
对于奖励积分,您不需要使用空格,您可以使用任何东西:
print('\r{0:?<{1}}'.format(repl, len(text)))
# Which becomes when extrapolated:
print('DELETED?????????????????????????????')
Or for extra BONUS make it into a function:
或者为了额外的奖励,把它变成一个函数:
from time import sleep
def overprint(text,repl, t=1, char=" "):
print(text, end="\r")
sleep(t)
print('\r{0:{1}<{2}}'.format(repl, char, len(text)))
overprint("The bomb will explode in one sec...", "BOOM!")