Python:使用回车和逗号打印不起作用 [英] python: print using carriage return and comma not working

查看:186
本文介绍了Python:使用回车和逗号打印不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要循环打印一行(Python 3.x).已经四处查看了,我在代码中添加了这一行:

I need to print over one line in a loop (Python 3.x). Looking around on SO already, I put this line in my code:

print('{0} imported\r'.format(tot),)

但是,它在循环时仍会打印多行.我也尝试过

However, it still prints multiple lines when looped through. I have also tried

sys.stdout.write('{0} imported\r'.format(tot))

但这不会在控制台上显示任何内容 ...

but this doesn't print anything to the console...

有人知道这是怎么回事吗?

Anyone know what's going on with this?

推荐答案

在第一种情况下,某些系统会将\r视为换行符.在第二种情况下,您没有flush该行.试试这个:

In the first case, some systems will treat \r as a newline. In the second case, you didn't flush the line. Try this:

sys.stdout.write('{0} imported\r'.format(tot))
sys.stdout.flush()

正如Levon提醒我的那样,

在所有系统上也不需要刷新该行-但通常以这种方式使用\r时是个好主意.

Flushing the line isn't necessary on all systems either, as Levon reminds me -- but it's generally a good idea when using \r this way.

这篇关于Python:使用回车和逗号打印不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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