Python unicode问题

时间:2011-03-21 21:42:42

标签: python unicode

将'\ xbb'转换为unicode字符串的正确方法是什么?我尝试了以下内容,只得到UnicodeDecodeError:

unicode('\xbb', 'utf-8')

'\xbb'.decode('utf-8')

3 个答案:

答案 0 :(得分:8)

由于它来自Word,它可能是CP1252。

>>> print '\xbb'.decode('cp1252')
»

答案 1 :(得分:1)

它看起来是拉丁文1编码的。你应该使用:

unicode('\ xbb','Latin-1')

答案 2 :(得分:0)

不确定你要做什么。但是在Python3中,默认情况下所有字符串都是unicode。在Python2.X中,您必须使用u'my unicode string \xbb'(或double,tripple quoted)来获取unicode字符串。当您想要打印unicode字符串时,您必须使用输出设备支持的字符集对它们进行编码,例如。终点站。例如u'my unicode string \xbb'.endoce('iso-8859-1')