[Tutor] re-directing print statements in exec
amk at amk.ca
amk at amk.ca
Thu Oct 9 20:48:40 EDT 2003
On Tue, Sep 23, 2003 at 11:49:14PM -0700, Toby Donaldson wrote:
> I am wondering if there is any easy way to make a print statement in an
> exec statement get sent to a string instead of the console. For
> instance,
Use the StringIO/cStringIO modules. (cStringIO is written in C,
theoretically making it faster, but unless you're writing lots of data it
probably won't make a significant difference.
Example:
from StringIO import StringIO
output = StringIO()
print >>output, 3.14*r**2
print >>output, 'blah blah blah'
str_value = output.getvalue()
--amk
More information about the Tutor
mailing list