简体   繁体   English

PyCharm print end='\\r' 语句不起作用

[英]PyCharm print end='\r' statement not working

There are already lots of other questions about the print statement, but I have not found an answer to my problem:已经有很多关于打印语句的其他问题,但我还没有找到我的问题的答案:

When I do:当我做:

for idx in range(10):
    print(idx, end="\r")

in the (ipython) terminal directly, it works fine and always overwrites the previous line.直接在(ipython)终端中,它工作正常并且总是覆盖上一行。 However, when running this in a module with PyCharm, I don't see any lines printed in the stdout.但是,当在带有 PyCharm 的模块中运行它时,我没有看到标准输出中打印出任何行。

Is this a known PyCharm issue?这是一个已知的 PyCharm 问题吗?

Try to add \\r at the beginning of your printed string (not at the end):尝试在打印字符串的开头(而不是结尾)添加\\r

    for idx in range(10):
        print('\r', idx, end='')

Carriage return at front, and end with '' to avoid new line '\\n'.前面回车,并以 '' 结尾以避免换行 '\\n'。 One solution to avoid the space is to use the format convention:避免空格的一种解决方案是使用格式约定:

    for idx in range(10):
        print("'\r{0}".format(idx), end='')

I had the same issue.我遇到过同样的问题。 While a solution using print() has eluded me, I have found sys.stdout.write works fine.虽然我没有找到使用 print() 的解决方案,但我发现 sys.stdout.write 工作正常。 (Win10, Python 3.5.1, Pycharm 2016.3.2) (Win10,Python 3.5.1,Pycharm 2016.3.2)

import sys
import time

def countdown(n):
    for x in reversed(range(n)):
        sys.stdout.write('\r' + str(x))
        time.sleep(1)

countdown(60)

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

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