Prev: need a json representation of all members of any type of object, list or hashtable (php's var_dump)
Next: how to let shell.SendKeys use unicode on windows in python
From: MRAB on 9 Aug 2010 22:49
Rene Veerman wrote:
> 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
old_stdout = sys.stdout
saved_output = StringIO()
sys.stdout = saved_output
print "Hello world!"
sys.stdout = old_stdout