[Tutor] htmllib, formatter and results as a string [StringIO]

Scott Griffitts sgriffitts@fwsymphony.org
Thu, 04 Oct 2001 10:08:43 -0500



>import sys, StringIO
>my_stringio =3D StringIO.StringIO()
>sys.stdout =3D my_stringio      ## redirect stdout to be our string file

>print "Hello world, do you see this?"

>sys.stdout =3D sys.__stdout__   ## set it back to the original stdout
>print "Here's what we captured from stdout:", my_stringio.getvalue()
>###

>This is a bit hacky, but it is sometimes necessary.  None of Python's
>standard library should force us to use this hack... so if you find
>something that does, tell us about it, and we'll complain.  *grin*

>Either way, StringIO is a lot of fun to play with.  Hope this helps!



Thanks, Danny.  StringIO works like a charm and should help me with some =
other problems I was having.

-Scott