Python将Unicode-Hex utf-8字符串转换为Unicode字符串 [英] Python Convert Unicode-Hex utf-8 strings to Unicode strings

查看:352
本文介绍了Python将Unicode-Hex utf-8字符串转换为Unicode字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有s = u'Gaga\xe2\x80\x99s',但需要转换为t = u'Gaga\u2019s'

如何最好地做到这一点?

How can this be best achieved?

推荐答案

在您解码原始字符串的任何地方,它都可能是使用latin-1或近亲进行解码的.由于latin-1是Unicode的前256个代码点,因此可以:

Where ever you decoded the original string, it was likely decoded with latin-1 or a close relative. Since latin-1 is the first 256 codepoints of Unicode, this works:

>>> s = u'Gaga\xe2\x80\x99s'
>>> s.encode('latin-1').decode('utf8')
u'Gaga\u2019s'

这篇关于Python将Unicode-Hex utf-8字符串转换为Unicode字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆