我有以下代码。最后一行抛出一个错误。为什么?在
class Foo(object):
def __unicode__(self):
return u'\u6797\u89ba\u6c11\u8b1d\u51b0\u5fc3\u6545\u5c45'
def __str__(self):
return self.__unicode__().encode('utf-8')
print "this works %s" % (u'asdf')
print "this works %s" % (Foo(),)
print "this works %s %s" % (Foo(), 'asdf')
print
print "this also works {0} {1}".format(Foo(), u'asdf')
print
print "this should break %s %s" % (Foo(), u'asdf')
错误为“UnicodeDecodeError:'ascii'编解码器无法解码位置18中的字节0xe6:序号不在范围(128)”
当您混合使用unicode和string对象时,python2隐式地尝试将unicode值编码为字符串,或它将尝试将字节字符串解码为unicode。在
您混合了unicode、字节字符串和自定义对象,并且触发了一系列不混合的编码和编码序列。在
当一个<1}时,{cdf><1><1><1><1>><1}。此解码失败,因为ASCII编解码器无法解码已经插值的
\xe6\x9e\x97
UTF-8字节序列。在在混合类型之前,您应该始终显式地将Unicode值编码为bytestrings,或者在混合类型之前将字节字符串解码为Unicode,因为角点的情况很复杂。在
显式地转换为
unicode()
起作用:当输出转换为unicode字符串时:
^{pr2}$否则你会得到一个字节串:
^{3}$(请注意,
asdf
也是bytestring)。在或者,使用unicode模板:
相关问题 更多 >
编程相关推荐