printing unicode strings

John Machin sjmachin at lexicon.net
Tue Jul 24 18:46:17 EDT 2007


On Jul 25, 6:56 am, 7stud <bbxx789_0... at yahoo.com> wrote:
> Can anyone tell me why I can print out the individual variables in the
> following code, but when I print them out combined into a single
> string, I get an error?
>
> symbol = u'ibm'
> price = u'4 \xbd'  # 4 1/2
>
> print "%s" % symbol
> print "%s" % price.encode("utf-8")
> print "%s %s" % (symbol, price.encode("utf-8") )
>
> --output:--
> ibm
> 4 1/2
> File "pythontest.py", line 6, in ?
>     print "%s %s" % (symbol, price.encode("utf-8") )
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position
> 2: ordinal not in range(128)

Because the first part is Unicode and the second part (after encoding
in utf8) is str.

It is trying to convert the second part to Unicode, using the default
codec (ascii), which of course must fail:

>>> price = u"4 \xbd"
>>> price.encode("utf8")
'4 \xc2\xbd'
>>>>>> price.encode("utf8").decode("ascii")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position
2: ordinal
not in range(128)
>>>





More information about the Python-list mailing list