今天碰见从数据库读取出来数据是u'xcaxd3xc6xb5xd7xa5xc8xa1',输出显示乱码,经常查询处理如下:
两种方式:
1、
s = u'xcaxd3xc6xb5xd7xa5xc8xa1'
a = s.encode('unicode_escape').decode('string_escape')
b = repr(a)
print unicode(eval(b),"gbk").encode('utf8')
输出:视频抓取
2、
s = u'xcaxd3xc6xb5xd7xa5xc8xa1'
a = s.encode('raw_unicode_escape')
b = repr(a)
print unicode(eval(b),"gbk").encode('utf8')
输出:视频抓取
def GetStrFromUnicode(self,ustr): ''' 把类似u'xcaxd3xc6xb5xd7xa5xc8xa1'的内容转为中文字符串 :param ustr: u'xcaxd3xc6xb5xd7xa5xc8xa1' :return: ''' result = ustr.encode('raw_unicode_escape') # result = ustr.encode('unicode_escape').decode('string_escape') uresult = unicode(eval(repr(result)), "gbk") return uresult.encode('utf8')