为什么python2.x抛出字符串格式+unicode的异常?

2024-09-27 23:24:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码。最后一行抛出一个错误。为什么?在

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)”


Tags: 代码selfreturnobjectfoodef错误unicode
1条回答
网友
1楼 · 发布于 2024-09-27 23:24:01

当您混合使用unicode和string对象时,python2隐式地尝试将unicode值编码为字符串,它将尝试将字节字符串解码为unicode。在

您混合了unicode、字节字符串和自定义对象,并且触发了一系列不混合的编码和编码序列。在

当一个<1}时,{cdf><1><1><1><1>><1}。此解码失败,因为ASCII编解码器无法解码已经插值的\xe6\x9e\x97UTF-8字节序列。在

在混合类型之前,您应该始终显式地将Unicode值编码为bytestrings,或者在混合类型之前将字节字符串解码为Unicode,因为角点的情况很复杂。在

显式地转换为unicode()起作用:

>>> print "this should break %s %s" % (unicode(Foo()), u'asdf')
this should break 林覺民謝冰心故居 asdf

当输出转换为unicode字符串时:

^{pr2}$

否则你会得到一个字节串:

^{3}$

(请注意,asdf也是bytestring)。在

或者,使用unicode模板

>>> u"this should break %s %s" % (Foo(), u'asdf')
u'this should break \u6797\u89ba\u6c11\u8b1d\u51b0\u5fc3\u6545\u5c45 asdf'

相关问题 更多 >

    热门问题