小编use*_*132的帖子

Python unicode错误

有人可以解释一下为什么在下面的例子中,print a 引发异常,而a.__str__()不是吗?

>>> class A:
...   def __init__(self):
...     self.t1 = "?akovec".decode("utf-8")
...     self.t2 = "tg"
...   def __str__(self):
...     return self.t1 + self.t2
... 
>>> a = A()
>>> print a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u010d' in position 0: ordinal not in range(128)
>>> a.__str__()
u'\u010dakovectg'
>>> print a.__str__()
?akovectg
Run Code Online (Sandbox Code Playgroud)

python python-unicode

1
推荐指数
1
解决办法
276
查看次数

标签 统计

python ×1

python-unicode ×1