> >>> import cStringIO
> >>> s = cStringIO.StringIO()
> >>> print >>s, [1, 2, 1.0/5, 'hello world']
> >>> s.getvalue()
Thanks--this works perfectly!
-_Steve
--
http://mail.python.org/mailman/listinfo/python-list
On Wed, 31 Oct 2007 22:17:48 +, mrstephengross wrote:
> I would like to get the results of a print operation placed in a string.
s = str(x)
If you specifically need to capture the output of print, then something
like this:
>>> import cStringIO
>>> s = cStringIO.StringIO()
>>> print >>s,
mrstephengross a écrit :
> I would like to get the results of a print operation
print is a statement, it doesn't yield any 'result'.
> placed in a
> string. For instance, you can very easily create a list and print it
> to stdout:
>
> x = [1,2,3]
> print x # Will print [1,2,3]
>
> What if
mrstephengross wrote:
> I would like to get the results of a print operation placed in a
> string. For instance, you can very easily create a list and print it
> to stdout:
>
> x = [1,2,3]
> print x # Will print [1,2,3]
>
> What if I want the text "[1,2,3]" placed in a string? For instance,
> s
I would like to get the results of a print operation placed in a
string. For instance, you can very easily create a list and print it
to stdout:
x = [1,2,3]
print x # Will print [1,2,3]
What if I want the text "[1,2,3]" placed in a string? For instance,
something like:
x = [1,2,3]
str =