我如何重定向python中的函数的打印输出 [英] How can I redirect print output of a function in python
问题描述
我在python中有一个将标准输出打印到标准输出的函数
def foo():
print(some text)
我想将在这个函数中打印的文本重定向到一个变量中,即'包装'这个函数或任何其他东西,以便文本存储在一个变量中:
text = wrapper(foo)
临时更改 sys.stdout
或者打开一个变量作为 FileObject
或者其他内容的强大方法? p>
>>> import sys
>>> import StringIO
>>> stdout = sys.stdout#在真实标准输出上保留句柄
>>> sys.stdout = StringIO.StringIO()#选择一个文件类对象写入
>>> foo()
>>> sys.stdout = stdout
>>> foo()
bar
我已经看到这样做甚至更好 - 您可以创建上下文管理器将stdout设置为任何你想要的输入上下文,然后在 __ exit __
上下文时使上下文管理器重置标准输出。
contextlib
创建上下文管理器: import contextlib
import sys
@ contextlib.contextmanager
def stdout_redirect(where):
sys.stdout = where
try:
yield其中
finally :
sys.stdout = sys .__ stdout__
def foo():
打印'bar'
#带有StringIO的示例
导入StringIO
$ b以stdout_redirect(StringIO.StringIO())作为new_stdout:
foo()
new_stdout.seek(0)
printdata from new_stdout: new_st dout.read()
new_stdout1 = StringIO.StringIO()
with stdout_redirect(new_stdout1):
foo()
new_stdout1.seek(0 )
打印new_stdout1:的数据,new_stdout1.read()
#现在用一个文件对象:
打开('new_stdout')作为f:
与stdout_redirect(f):
foo()
#只是为了证明我们实际上已经把stdout放回原来的状态
print现在调用没有上下文的foo
foo()
注意:
在python3.x中, StringIO.StringIO
已经转移到 io.StringIO
。另外,在python2.x中, cStringIO.StringIO
的性能可能稍高一些。
Possible Duplicate:
Can I redirect the stdout in python into some sort of string buffer?
I have a function in python that prints something to the standard output
def foo():
print("some text")
I want to 'redirect' the text that is being printed in this function into a variable, i.e. 'wrap' this function or whatever so that the text is stored in a variable:
text = wrapper(foo)
Is there a robust way to temporarily change sys.stdout
or to open a variable as a FileObject
, or something else?
>>> import sys
>>> import StringIO
>>> stdout = sys.stdout # keep a handle on the real standard output
>>> sys.stdout = StringIO.StringIO() # Choose a file-like object to write to
>>> foo()
>>> sys.stdout = stdout
>>> foo()
bar
I've seen this done even better -- You can create a context manager to set stdout to whatever you want when you enter the context and then have the context manager reset stdout when you __exit__
the context.
Here's a simple example using contextlib
to create the context manager:
import contextlib
import sys
@contextlib.contextmanager
def stdout_redirect(where):
sys.stdout = where
try:
yield where
finally:
sys.stdout = sys.__stdout__
def foo():
print 'bar'
# Examples with StringIO
import StringIO
with stdout_redirect(StringIO.StringIO()) as new_stdout:
foo()
new_stdout.seek(0)
print "data from new_stdout:",new_stdout.read()
new_stdout1 = StringIO.StringIO()
with stdout_redirect(new_stdout1):
foo()
new_stdout1.seek(0)
print "data from new_stdout1:",new_stdout1.read()
# Now with a file object:
with open('new_stdout') as f:
with stdout_redirect(f):
foo()
# Just to prove that we actually did put stdout back as we were supposed to
print "Now calling foo without context"
foo()
Note:
On python3.x, StringIO.StringIO
has moved to io.StringIO
. Also, on python2.x, cStringIO.StringIO
might be slightly more performant.
这篇关于我如何重定向python中的函数的打印输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!