StringIO in 2.6 and beyond

Jean-Paul Calderone exarkun at divmod.com
Wed Dec 10 09:05:50 EST 2008


On 10 Dec 2008 11:58:37 GMT, Bill McClain <20080915.20.wmcclain at spamgourmet.com> wrote:
>On 2008-12-10, ajaksu <ajaksu at gmail.com> wrote:
>> On Dec 9, 5:24 pm, Bill McClain <20080915.20.wmccl... at spamgourmet.com>
>> wrote:
>> > On 2008-12-09, MRAB <goo... at 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.
>
>> Try:
>
>> from __future__ import unicode_literals
>
>That works for:
>
>    output.write('First line.\n')
>
>...but not for:
>
>   print('Second line.', file=output)
>
>Maybe a combination of this and functools.partial as was suggested before. At
>least the necessary edits would be at the top of the program.

See http://bugs.python.org/issue4618, there's a comment with a workaround
for this problem.

Jean-Paul



More information about the Python-list mailing list