need a json representation of all members of any type of object, list or hashtable (php's var_dump)

MRAB python at mrabarnett.plus.com
Mon Aug 9 22:49:47 EDT 2010


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()
try:
     sys.stdout = saved_output
     print "Hello world!"
finally:
     sys.stdout = old_stdout

print saved_output.getvalue()



More information about the Python-list mailing list