考虑在Python中使用print的以下示例:
>>> for i in range(4): print('.')
.
.
.
.
>>> print('.', '.', '.', '.')
. . . .
在每个值之间添加换行符或空格。我怎样才能避免这种情况,从而使输出。。。。相反换句话说,如何将字符串“附加”到标准输出流?
考虑在Python中使用print的以下示例:
>>> for i in range(4): print('.')
.
.
.
.
>>> print('.', '.', '.', '.')
. . . .
在每个值之间添加换行符或空格。我怎样才能避免这种情况,从而使输出。。。。相反换句话说,如何将字符串“附加”到标准输出流?
当前回答
为Python 2.6+使用Python 3样式打印函数(它还将打破同一文件中任何现有的带关键字的打印语句)。
# For Python 2 to use the print() function, removing the print keyword
from __future__ import print_function
for x in xrange(10):
print('.', end='')
为了不破坏所有Python 2打印关键字,请创建一个单独的printf.py文件:
# printf.py
from __future__ import print_function
def printf(str, *args):
print(str % args, end='')
然后,在文件中使用它:
from printf import printf
for x in xrange(10):
printf('.')
print 'done'
#..........done
显示printf样式的更多示例:
printf('hello %s', 'world')
printf('%i %f', 10, 3.14)
#hello world10 3.140000
其他回答
或具有如下功能:
def Print(s):
return sys.stdout.write(str(s))
现在:
for i in range(10): # Or `xrange` for the Python 2 version
Print(i)
输出:
0123456789
您可以尝试:
import sys
import time
# Keeps the initial message in buffer.
sys.stdout.write("\rfoobar bar black sheep")
sys.stdout.flush()
# Wait 2 seconds
time.sleep(2)
# Replace the message with a new one.
sys.stdout.write("\r"+'hahahahaaa ')
sys.stdout.flush()
# Finalize the new message by printing a return carriage.
sys.stdout.write('\n')
莱诺满足了我的要求。我在搜索“python suppress newline”时发现了这篇文章。我正在树莓派上使用IDLE 3为PuTTY开发Python 3.2。
我想在PuTTY命令行上创建一个进度条。我不希望页面滚动。我想要一条水平线,让用户放心,不要担心程序没有停止,也没有在一个快乐的无限循环中被送去吃午饭,以此恳求“离开我吧,我做得很好,但这可能需要一些时间。”交互式消息-如文本中的进度条。
打印('Shamming for',search_string,'\b!.001',end='')通过准备下一次屏幕写入来初始化消息,这将打印三个退格作为⌫\9003;\9003搓出,然后打印一个句点,删除“001”并延长句点行。
search_string鹦鹉学舌用户输入后,\b!修剪search_string文本的感叹号,使其位于print()强制的空格上方,并正确放置标点符号。然后是空格和我正在模拟的“进度条”的第一个“点”。
不必要的是,消息也会以页码开头(格式为三,带前导零),以通知用户正在处理进度,这也将反映我们稍后将在右侧构建的周期数。
import sys
page=1
search_string=input('Search for?',)
print('Skimming for', search_string, '\b! .001', end='')
sys.stdout.flush() # the print function with an end='' won't print unless forced
while page:
# some stuff…
# search, scrub, and build bulk output list[], count items,
# set done flag True
page=page+1 #done flag set in 'some_stuff'
sys.stdout.write('\b\b\b.'+format(page, '03')) #<-- here's the progress bar meat
sys.stdout.flush()
if done: #( flag alternative to break, exit or quit)
print('\nSorting', item_count, 'items')
page=0 # exits the 'while page' loop
list.sort()
for item_count in range(0, items)
print(list[item_count])
#print footers here
if not (len(list)==items):
print('#error_handler')
进度条肉在sys.stdout.write('\b\b\b.'+格式(第页,“03”)行中。首先,要向左擦除,它将光标备份到三个数字字符上,并将“\b\b\b”作为“⌫\9003 \9003;”,然后删除一个新的句点以添加到进度条长度。然后,它将写入到目前为止已完成的页面的三位数。因为sys.stdout.write()等待满缓冲区或输出通道关闭,所以sys.stdut.flush()强制立即写入。sys.stdout.flush()内置在打印结束()中,该结束被打印(txt,end='')绕过。然后,代码循环执行普通的时间密集型操作,而不打印任何内容,直到它返回此处,将三位数字擦回去,添加一个句点,然后再次写入三位数字,递增。
擦除和重写的三个数字绝非必要——这只是一个繁荣的例子,它是sys.stdout.write()与print()的对比。你也可以很容易地用句点填充,并忘记三个奇怪的反斜杠-b⌫退格(当然,也不写格式化的页计数),只需每次打印一个句点条,而不用空格或换行,只需使用sys.stdout.write('.');sys.stdout.flush()对。
请注意,Raspberry Pi IDLE 3 Python外壳不将退格视为⌫rubout,而是打印一个空格,创建一个明显的分数列表。
我最近也有同样的问题。。。
我通过以下方式解决了这个问题:
import sys, os
# Reopen standard output with "newline=None".
# in this mode,
# Input: accepts any newline character, outputs as '\n'
# Output: '\n' converts to os.linesep
sys.stdout = os.fdopen(sys.stdout.fileno(), "w", newline=None)
for i in range(1,10):
print(i)
这在Unix和Windows上都有效,但我还没有在Mac OS X上测试过。
为Python 2.6+使用Python 3样式打印函数(它还将打破同一文件中任何现有的带关键字的打印语句)。
# For Python 2 to use the print() function, removing the print keyword
from __future__ import print_function
for x in xrange(10):
print('.', end='')
为了不破坏所有Python 2打印关键字,请创建一个单独的printf.py文件:
# printf.py
from __future__ import print_function
def printf(str, *args):
print(str % args, end='')
然后,在文件中使用它:
from printf import printf
for x in xrange(10):
printf('.')
print 'done'
#..........done
显示printf样式的更多示例:
printf('hello %s', 'world')
printf('%i %f', 10, 3.14)
#hello world10 3.140000