PyCharm Python 控制台 - 在同一行打印未按预期工作 [英] PyCharm Python Console - Printing on the same line not working as intended

查看:28
本文介绍了PyCharm Python 控制台 - 在同一行打印未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是通过用当前进度覆盖上一行来在我的控制台中显示加载进度.我为 Python 3 版找到了很多解决方案,但这些都不起作用.

My goal is to show a loading progress in my console by overwriting the previous line with the current progress. I have found plenty of solutions for version 3 of Python, but those are not working.

例如:

import time
for i in range(10):
    print(i, end='', flush=True)
    time.sleep(1)

给我以下输出:

0123456789

或两者兼有:

import time
for i in range(10):
    print(i, end='
', flush=True)
    time.sleep(1)

和:

import time
for i in range(10):
    print("" + str(i), end='', flush=True)
    time.sleep(1)

给我以下输出:

0
1
2
3
...

有什么想法吗?我正在使用 Anaconda 包在 PyCharm 社区版下工作.

Any idea ? I am working under PyCharm Community Edition with the Anaconda package.

非常感谢!

当我运行 python 文件(使用 PyCharm)时,问题似乎没有发生,但只有在我执行在控制台中执行选择"时才会发生

the problem does not seem to happen when I run a python file (using PyCharm), but only when I do "Execute Selection in Console"

推荐答案

您需要控制写入标准输出并刷新它.所以ma3oun给出的链接是有价值的答案.不幸的是,PyCharm 控制台的工作方式与命令行不同.应将其如何工作的问题直接提交给 PyCharm 团队,并可能要求他们改变这种行为.

You need to get control over writing to stdout and flushing it. So link given by ma3oun is valuable answer. Unfortunately PyCharm console works differently than command line. Question how it works shall be directed to PyCharm team and maybe ask them to change this behaviour.

无论如何,您的问题的解决方案是在执行写入之前添加 :

Anyway solution for your problem is to add before you perform write:

import sys
import time

for i in range(10):
    sys.stdout.write("
{0}".format(str(i)))
    sys.stdout.flush()
    time.sleep(1)

但是,对于较短的睡眠时间(例如 0.1),刷新短缓冲区会出现问题.对于每次刷新,您获得的不仅仅是一个写入值.作为一种解决方法,我发现您可以像这样添加另一个 :

However for shorter sleep time (for instance 0.1) there's a problem with flushing short buffer. For every flush you get more than just one write value printed. As a workaround I found that you can add another like that:

for i in range(10):
    sys.stdout.write("
 
 {0}".format(str(i)))
    sys.stdout.flush()
    time.sleep(0.1)

这将强制控制台仅显示一个写入值,但您会在显示之间丢失一些值,因为看起来刷新率低于 0.1 秒.

This will force console to display only one write value, but you will loose some of values in between display as it looks like refresh rate is slower than 0,1 of a second.

我在 PyCharm 控制台中正确显示进度条的解决方案:

My solution for progress banner that is properly displayed in PyCharm console:

import sys
import time

progressVis = {0: '          ', 1: '-         ', 2: '--        ', 3: '---       ', 4: '----      ', 5: '-----     ',
               6: '------    ', 7: '-------   ', 8: '--------  ', 9: '--------- ', 10: '----------'}

size = 20
for i in range(0, size):
    percent = int((float(i + 1) / size) * 10)
    str1 = "
 
 [{0}] {1}/{2} {3}%".format(progressVis[percent],
                                                 i + 1, size,
                                                 ((i + 1) * 100 / size))
    sys.stdout.write(str1)
    sys.stdout.flush()
    time.sleep(0.1)

这篇关于PyCharm Python 控制台 - 在同一行打印未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆