I am using Python 2.7.13(in Kali 4.9.0 amd64), and I have tried every variation of the print() statement that I can find(with the proper future import statements), and I am ending up with OK on a newline everytime. Here is the code:
我使用的是Python2.7.13(在Kali 4.9.0AMD64中),并且我已经尝试了我能找到的print()语句的所有变体(使用未来正确的导入语句),每次都以换行符OK结束。以下是代码:
from __future__ import print_function
for i in range(0, 10000):
print("\rOK", end="")
I have tried:
我试过了:
single quotes instead of double for end=""
End=“”的单引号而不是双引号
using Python 2.x syntax instead of importing print_function and using 3.0 syntax
使用Python2.x语法,而不是导入print_Function并使用3.0语法
end='\r' end="\r"
End=‘\r’end=“\r”
a space in between the quotes of end=""
end="”的引号之间的空格
When I used the 2.7 syntax, I was able to get the output to stay on the same line but still printing in a row, when I try to use a carriage return to return the cursor to the beginning position(and overwrite previous output), it inserts a newline, even with a terminating comma in my print statement.
I have read Question 3249524, along with well over a dozen other explanations for this problem. I have also tried using the sys.stdout functions instead:
当我使用2.7语法时,我能够使输出停留在同一行上,但仍在一行中打印,当我尝试使用回车符将游标返回到开始位置(并覆盖先前的输出)时,它会插入一个换行符,即使我的print语句中有一个终止逗号。我已经阅读了问题3249524,以及对这个问题的十几个其他解释。我还尝试改用sys.stdout函数:
import sys
for i in range(10):
sys.stdout.write("\rCountdown: %d" % i)
sys.stdout.flush()
The output of both programs is still on a newline every time. I am at a loss as to how to get it to print in the same place on the same line. Thanks to anyone who can illuminate this issue.
这两个程序的输出每次都在换行符上。我不知道怎样才能把它印在同一行的同一位置。感谢任何能阐明这个问题的人。
更多回答
What shell/terminal manager are you using? They may not properly handle carriage returns. I can make this work on, for example bash on Ubuntu on Windows just fine, as long as I put a time.sleep
in between each print so I can see the numbers change slowly.
您使用的是哪种外壳/终端管理器?它们可能无法正确处理回车。我可以让它工作,例如,在Windows上的Ubuntu上bash很好,只要我花点时间。在每一张照片之间休息,这样我就可以看到数字慢慢变化。
ShadowRanger I was using Spyder but now I am running everything from bash directly and have completely uninstalled Spyder and stopped using it. The carriage return works exactly as it should when running the script from the command line, I suspect a lot of the people having the carriage return issue are running their scripts directly in their IDE console, and not from the command line.
ShadowRanger我一直在使用Spyder,但现在我直接从bash运行所有东西,并且已经完全卸载了Spyder并停止使用它。当从命令行运行脚本时,回车符的工作方式完全一样,我怀疑许多有回车符问题的人是直接在他们的IDE控制台中运行他们的脚本,而不是从命令行运行。
That's exactly why I asked. I've heard of similar issues in a variety of custom shells (many of them integrated into IDEs). You also see issues with multiprocessing
scripts in many cases.
这就是我问你的原因。我听说过在各种定制的外壳中出现过类似的问题(其中许多都集成到了IDE中)。在许多情况下,您还会看到多处理脚本的问题。
优秀答案推荐
For me, I wanted to display some text scrolling sideways. Putting the \r
at the beginning of the print statement was what I was looking for - if you put it at the end of the line nothing is displayed. I guess the carriage return behaves line like a Ctrl-u
and not a true carriage return. :-/
对我来说,我想要显示一些横向滚动的文本。我希望将\r放在print语句的开头--如果您将它放在行尾,则不会显示任何内容。我猜回车符的行为类似于Ctrl-u,而不是真正的回车符。:-/
Here's my example...
这是我的例子。
import time
def sideways_scroll(string, display_length):
string_length = len(string)
for i in range(string_length):
subset = string[i:i + display_length]
print(f'\r{subset}', end='', flush=True)
time.sleep(0.5) # Adjust the delay time as desired
# Example usage
string = 'This is a Simple Sideways Scrolling "Hello, World" Message! '
display_length = 5
sideways_scroll(string, display_length)
更多回答
我是一名优秀的程序员,十分优秀!