Python 打印时出现UNICODEENCODEER错误

Python 打印时出现UNICODEENCODEER错误,python,error-handling,encode,Python,Error Handling,Encode,我有一个类SomeClass,我想在调试时打印这个类: def __repr__(self): print type(self.id) print type(self.cn_name) print type(self.name) print type(self.finished) return u''' Bangumi id: %s Chinese Name: %s Original Name: %s Finished or

我有一个类SomeClass,我想在调试时打印这个类:

def __repr__(self):
    print type(self.id)
    print type(self.cn_name)
    print type(self.name)
    print type(self.finished)
    return u'''
    Bangumi id: %s
    Chinese Name: %s
    Original Name: %s
    Finished or Not: %s''' % (self.id, self.cn_name, self.name, self.finished)
我得到以下信息:

>>> print anime.__repr__
<type 'int'>
<type 'unicode'>
<type 'unicode'>
<type 'int'>
Traceback (most recent call last):

File "<debugger>", line 1, in <module>
print anime.__repr__
UnicodeEncodeError: 'ascii' codec can't encode characters in position 45-50: ordinal not in range(128)
打印动画__ 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 打印动画__ UnicodeEncodeError:“ascii”编解码器无法对位置45-50处的字符进行编码:序号不在范围内(128)
这是什么意思?如何恢复它?

方法
必须返回字节字符串对象;a
str
。而是返回一个
unicode
对象,Python使用ASCII编解码器对其进行隐式编码,以将其强制为字符串

顺便说一句,如果你真的调用了
anime,这种情况就不会发生了;相反,您只是指方法对象,它的表示形式包括一个
repr(anime)
字符串,执行编码的是
repr()
函数

您可以通过不从方法返回Unicode来修复此问题。将返回值显式编码为
str


添加
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。请参见您的
\uuuu repr\uuuu
方法正确返回了Unicode字符串,但您的控制台或终端配置不正确,Python试图将结果编码为ASCII。您使用的是哪种终端或控制台?请注意,某些IDE控制台不能正确地告诉Python使用什么编码。请注意,您永远不应该从
\uuuuuu repr\uuuuuuu
返回Unicode;Python2中的所有其他内容都会返回bytestring,请参见。这是另一个问题;您的控制台也不会打印任何其他unicode。@MartijnPieters我正在使用最新版本的PyCharmCE。这就是问题所在吗?@MartijnPieters终端也是错误的。(我使用的是OSX10.9,语言是中文)