From: MRAB on
Rene Veerman wrote:
> hi.
> for 2 open source components of mine that are used in debugging, i
> would like to capture the output generated by print for any variable.
> i'm new to python, so please excuse my noobishness.
> i don't know if objects can be json-ed as easy as lists and hash
> tables are by print.
> and i couldn't find how to capture the output generated by print into
> a string, so i can't use it with google appengine :(
> i'm in dire need of some expert advise here.
Lookup the 'StringIO' module (Python 2.x):

from StringIO import StringIO
import sys

old_stdout = sys.stdout
saved_output = StringIO()
sys.stdout = saved_output
print "Hello world!"
sys.stdout = old_stdout

print saved_output.getvalue()