python中捕获到stdout的输出,实际上就是把sys.stdout只想到一个打开的文件即可
import StringIO
import string, sys
stdout = sys.stdout
sys.stdout = file = StringIO.StringIO()
print """
According to Gbaya folktales, trickery and guile
are the best ways to defeat the python, king of
snakes, which was hatched from a dragon at the
world's start. -- National Geographic, May 1997
"""
sys.stdout = stdout
print string.upper(file.getvalue())
类似与php中的ob_*系列函数。
It's like comparing apples to oranges.