use*_*108 212 python printing stdout line-breaks
我想运行一个脚本,它基本上显示如下内容:
Installing XXX... [DONE]
Run Code Online (Sandbox Code Playgroud)
现在,此刻,我在功能成功后使用print打印整行.但是,我现在希望它首先打印"安装xxx ...",并在功能运行之后添加"DONE"标签; 但是在同一条线上.
有任何想法吗?
mul*_*ces 267
您可以使用该print语句执行此操作而无需导入sys.
def install_xxx():
print "Installing XXX... ",
install_xxx()
print "[DONE]"
Run Code Online (Sandbox Code Playgroud)
该print行末尾的逗号阻止print发出新行(您应该注意输出末尾会有一个额外的空格).
Python 3解决方案
由于上述内容在Python 3中不起作用,您可以改为执行此操作(同样,不进行导入sys):
def install_xxx():
print("Installing XXX... ", end="", flush=True)
install_xxx()
print("[DONE]")
Run Code Online (Sandbox Code Playgroud)
print函数接受end默认的参数"\n".将其设置为空字符串可防止它在该行的末尾发出新行.
bol*_*nik 102
你可以简单地使用这个:
print 'something',
...
print ' else',
Run Code Online (Sandbox Code Playgroud)
并且输出将是
something else
Run Code Online (Sandbox Code Playgroud)
不需要过度杀戮import sys.最后注意逗号符号.
Python 3+
print("some string", end="");在最后删除换行插入.阅读更多help(print);
Vad*_*4uk 45
正确答案!您必须使用退格' \ r '或(' \ x08 ')char来返回控制台输出中的先前位置
Python 2+:
import time
import sys
def backspace(n):
sys.stdout.write((b'\x08' * n).decode()) # use \x08 char to go back
for i in range(101): # for 0 to 100
s = str(i) + '%' # string for output
sys.stdout.write(s) # just print
sys.stdout.flush() # needed for flush when using \x08
backspace(len(s)) # back n chars
time.sleep(0.2) # sleep for 200ms
Run Code Online (Sandbox Code Playgroud)
Python 3:
import time
def backline():
print('\r', end='') # use '\r' to go back
for i in range(101): # for 0 to 100
s = str(i) + '%' # string for output
print(s, end='') # just print and flush
backline() # back to the beginning of line
time.sleep(0.2) # sleep for 200ms
Run Code Online (Sandbox Code Playgroud)
此代码在一行上将从0%计算到100%.最终价值将是:
> python test.py
100%
Run Code Online (Sandbox Code Playgroud)
在这种情况下有关flush的其他信息:为什么包含'end ='参数的python print语句在while循环中表现不同?
fer*_*tar 43
使用sys.stdout.write('Installing XXX... ')和sys.stdout.write('Done').这样,"\n"如果要重新创建打印功能,则必须手动添加新行.我认为可能没有必要为此使用curses.
inc*_*ick 16
没有任何答案对我有用,因为它们都会暂停,直到遇到新的一行.我写了一个简单的帮手:
def print_no_newline(string):
import sys
sys.stdout.write(string)
sys.stdout.flush()
Run Code Online (Sandbox Code Playgroud)
测试它:
import time
print_no_newline('hello ')
# Simulate a long task
time.sleep(2)
print('world')
Run Code Online (Sandbox Code Playgroud)
"你好"将首先打印出来并在睡眠前冲到屏幕上.之后,您可以使用标准打印.
Joh*_*tta 14
sys.stdout.write 将打印而不返回运输
import sys
sys.stdout.write("installing xxx")
sys.stdout.write(".")
Run Code Online (Sandbox Code Playgroud)
http://en.wikibooks.org/wiki/Python_Programming/Input_and_output#printing_without_commas_or_newlines
MrK*_*lli 10
Python 附加换行符作为打印的结尾。使用 end=' ' for python3 for print 方法来附加空格而不是换行符。对于 python2,在打印语句的末尾使用逗号。
print('Foo', end=' ')
print('Bar')
Run Code Online (Sandbox Code Playgroud)
小智 9
这个简单的例子将在同一行打印1-10.
for i in range(1,11):
print (i, end=" ")
Run Code Online (Sandbox Code Playgroud)
打印有一个可选的结束参数,它是最后打印的.默认值为换行符,但您可以将其更改为空字符串.例如:print("hello world!",end ="")
print(‘\r’+’something to be override’,end=‘’)
Run Code Online (Sandbox Code Playgroud)
这意味着它将把光标返回到开始处,而不是打印一些内容并在同一行结束。如果处于循环中,它将在开始的位置开始打印。
| 归档时间: |
|
| 查看次数: |
444900 次 |
| 最近记录: |