From: Bill McClain on
On 2008-12-09, pruebauno(a)latinmail.com <pruebauno(a)latinmail.com> wrote:

> 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.

This works:

output.write(unicode('First line.\n'))

...but this generates the error:

print(unicode('Second line.'), file=output)

-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: Peter Otten 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?

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

Seems io.StringIO() wants unicode. So let's feed it some:

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

Still complaining? Let's have a look at the output so far:

>>> out.getvalue()
u'hello'

Hmm, u"hello" was written. The complaint must be about the newline then.

>>> out = io.StringIO()
>>> print(u"hello", file=out, end=u"\n")
>>> out.getvalue()
u'hello\n'

Heureka. Let's try something else now:

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

Fixing is left as exercise ;)

Peter
From: Bill McClain on
On 2008-12-09, Peter Otten <__peter__(a)web.de> wrote:

> >>> out = io.StringIO()
> >>> print(u"hello", file=out, end=u"\n")
> >>> out.getvalue()
> u'hello\n'

That has the benefit of working. Thank you!

That can't be the intended behavior of print(), can it? Insering non-unicode
spaces and line terminators? I thought all text was unicode now. Or is that
only in 3.0?

-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: MRAB on
Bill McClain wrote:
> On 2008-12-09, Peter Otten <__peter__(a)web.de> wrote:
>
>>>>> out = io.StringIO()
>>>>> print(u"hello", file=out, end=u"\n")
>>>>> out.getvalue()
>> u'hello\n'
>
> That has the benefit of working. Thank you!
>
> That can't be the intended behavior of print(), can it? Insering non-unicode
> spaces and line terminators? I thought all text was unicode now. Or is that
> only in 3.0?
>
In Python 2.x unmarked string literals are bytestrings. In Python 3.x
they're Unicode. The intention is to make the transition from 2.x to 3.x
easier by adding some features of 3.x to 2.x, but without breaking
backwards compatibility (not entirely successfully!).
From: Bill McClain on
On 2008-12-09, MRAB <google(a)mrabarnett.plus.com> wrote:

> In Python 2.x unmarked string literals are bytestrings. In Python 3.x
> they're Unicode. The intention is to make the transition from 2.x to 3.x
> easier by adding some features of 3.x to 2.x, but without breaking
> backwards compatibility (not entirely successfully!).

It is a bit ugly. In 2.6 StringIO won't take bytestrings, so I apply u'x'. But
in 3.0 u'x' will be gone and I'll have to change the code again.

It's a small effort for what I am working on, but frustrating trying to puzzle
this out from the docs, which I couldn't do.

-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