在控制台中正确打印\r [英] Print \r correctly in console

查看:245
本文介绍了在控制台中正确打印\r的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编写更新打印行的脚本时,例如:

When I write a script that updates a printed line, for example like this:

for i in range(101):
    print(str(i) + "% \r", end="")

并使用终端(Ubuntu)运行此脚本,我得到正确的输出,更新了这行:

and run this script using the terminal (Ubuntu), I get the correct output, that updated the line:

100%

但是,在Eclipse中使用Pydev,Eclipse控制台执行以下操作:

However, using Pydev in Eclipse, the Eclipse console does this:

0%
1%
2%
...
100%

任何人都知道如何解决这个问题?感谢提前!

Anyone know how to fix this? Thanks in advance!

推荐答案

这似乎是旧的CR LF问题。根据您使用的操作系统和控制台,CR和LF作为线路终止将被不同地解释。

This seems to be the old CR LF problem. Depending on the OS and the console you are using, CR and LF as a line termination will be interpreted differently.

某些系统需要CRLF作为行尾。
有些系统只需要LF,而是隐式地执行CR。
有些系统(像你的)在每个CR之前都会隐藏一个LF,虽然这是我第一次看到这个。

Some systems require a CRLF as an end of line. Some systems only require LF but do the CR implicitly. Some systems (like yours) do a LF before each CR implicitly, although this is the first time I see this.

也许有一种方法来编辑您的PyDev控制台的换行设置。

Maybe there is a way to edit the newline settings for your PyDev console.

编辑:或者您可以使用ANSI转义码来移动光标。像CSInD的左侧为n个字符或CSInC为n个字符在右边。

Or you might use ANSI escape codes for moving the cursor around. Like CSInD for n characters to the left or CSInC for n characters to the right.

这篇关于在控制台中正确打印\r的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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