From: Bill McClain on
I've just installed 2.6, had been using 2.4.

This was working for me:

#! /usr/bin/env python
import StringIO
out = StringIO.StringIO()
print >> out, 'hello'

I used 2to3, and added import from future to get:

#! /usr/bin/env python
from __future__ import print_function
import io
out = io.StringIO()
print('hello', file=out)

....which gives an error:

Traceback (most recent call last):
File "./example.py", line 5, in <module>
print('hello', file=out)
File "/usr/local/lib/python2.6/io.py", line 1487, in write
s.__class__.__name__)
TypeError: can't write str to text stream

....which has me stumped. Why can't it?

-Bill
--
Sattre Press History of Astronomy
http://sattre-press.com/ During the 19th Century
info(a)sattre-press.com by Agnes M. Clerke
http://sattre-press.com/han.html
From: Christian Heimes on
Bill McClain wrote:
> I've just installed 2.6, had been using 2.4.
>
> This was working for me:
>
> #! /usr/bin/env python
> import StringIO
> out = StringIO.StringIO()
> print >> out, 'hello'
>
> I used 2to3, and added import from future to get:
>
> #! /usr/bin/env python
> from __future__ import print_function
> import io
> out = io.StringIO()
> print('hello', file=out)
>
> ....which gives an error:
>
> Traceback (most recent call last):
> File "./example.py", line 5, in <module>
> print('hello', file=out)
> File "/usr/local/lib/python2.6/io.py", line 1487, in write
> s.__class__.__name__)
> TypeError: can't write str to text stream
>
> ....which has me stumped. Why can't it?

In this context 'str' means Python 3.0's str type, which is unicode in
2.x. Please report the misleading error message.

Christian

From: Bill McClain on
On 2008-12-08, Christian Heimes <lists(a)cheimes.de> wrote:

> In this context 'str' means Python 3.0's str type, which is unicode in
> 2.x. Please report the misleading error message.

So this is an encoding problem? Can you give me a hint on how to correct in my
example? I see that io.StringIO() has an encoding parameter, but I'm unclear
what to specify.

-Bill
--
Sattre Press History of Astronomy
http://sattre-press.com/ During the 19th Century
info(a)sattre-press.com by Agnes M. Clerke
http://sattre-press.com/han.html
From: Bill McClain on
On 2008-12-08, Bill McClain <20080915.20.wmcclain(a)spamgourmet.com> wrote:
> On 2008-12-08, Christian Heimes <lists(a)cheimes.de> wrote:

> > In this context 'str' means Python 3.0's str type, which is unicode in
> > 2.x. Please report the misleading error message.

> So this is an encoding problem? Can you give me a hint on how to correct in my
> example? I see that io.StringIO() has an encoding parameter, but I'm unclear
> what to specify.

I still don't have this working. I've been specifying encodings without
success.

The StringIO example usage in the Python 3.0 documentation here:

http://docs.python.org/3.0/library/io.html#io.StringIO

gives me the same error on 2.6:

#! /usr/bin/env python

from __future__ import print_function
import io

output = io.StringIO()
output.write('First line.\n')
print('Second line.', file=output)

# Retrieve file contents -- this will be
# 'First line.\nSecond line.\n'
contents = output.getvalue()

# Close object and discard memory buffer --
# .getvalue() will now raise an exception.
output.close()

../stringio30.py
Traceback (most recent call last):
File "./stringio30.py", line 7, in <module>
output.write('First line.\n')
File "/usr/local/lib/python2.6/io.py", line 1487, in write
s.__class__.__name__)
TypeError: can't write str to text stream

-Bill
--
Sattre Press History of Astronomy
http://sattre-press.com/ During the 19th Century
info(a)sattre-press.com by Agnes M. Clerke
http://sattre-press.com/han.html
From: pruebauno on
On Dec 9, 11:28 am, Bill McClain
<20080915.20.wmccl...(a)spamgourmet.com> wrote:
> On 2008-12-08, Bill McClain <20080915.20.wmccl...(a)spamgourmet.com> wrote:
>
> > On 2008-12-08, Christian Heimes <li...(a)cheimes.de> wrote:
> > > In this context 'str' means Python 3.0's str type, which is unicode in
> > > 2.x. Please report the misleading error message.
> > So this is an encoding problem? Can you give me a hint on how to correct in my
> > example? I see that io.StringIO() has an encoding parameter, but I'm unclear
> > what to specify.
>
> I still don't have this working. I've been specifying encodings without
> success.
>
> The StringIO example usage in the Python 3.0 documentation here:
>
>      http://docs.python.org/3.0/library/io.html#io.StringIO
>
> gives me the same error on 2.6:
>
>     #! /usr/bin/env python
>
>     from __future__ import print_function
>     import io
>
>     output = io.StringIO()
>     output.write('First line.\n')
>     print('Second line.', file=output)
>
>     # Retrieve file contents -- this will be
>     # 'First line.\nSecond line.\n'
>     contents = output.getvalue()
>
>     # Close object and discard memory buffer --
>     # .getvalue() will now raise an exception.
>     output.close()
>
> ./stringio30.py
> Traceback (most recent call last):
>   File "./stringio30.py", line 7, in <module>
>     output.write('First line.\n')
>   File "/usr/local/lib/python2.6/io.py", line 1487, in write
>     s.__class__.__name__)
> TypeError: can't write str to text stream
>
> -Bill
> --
> Sattre Press                              History of Astronomyhttp://sattre-press.com/              During the 19th Century
> i...(a)sattre-press.com                       by Agnes M. Clerke
>                              http://sattre-press.com/han.html

This puzzles me too. According to the documentation StringIO accepts
both byte strings and unicode strings. Try to replace
output.write('First line.\n')
with
output.write(unicode('First line.\n'))
or
output.write(str('First line.\n'))
and see if one of those works.