开发者

Python Convert Unicode-Hex utf-8 strings to Unicode strings

开发者 https://www.devze.com 2023-04-09 07:17 出处:网络
Have s 开发者_如何转开发= u\'Gaga\\xe2\\x80\\x99s\' but need to convert to t = u\'Gaga\\u2019s\'

Have s 开发者_如何转开发= u'Gaga\xe2\x80\x99s' but need to convert to t = u'Gaga\u2019s'

How can this be best achieved?


s = u'Gaga\xe2\x80\x99s'
t = u'Gaga\u2019s'
x = s.encode('raw-unicode-escape').decode('utf-8')
assert x==t

print(x)

yields

Gaga’s


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'


import codecs

s = u"Gaga\xe2\x80\x99s"
s_as_str = codecs.charmap_encode(s)[0]
t = unicode(s_as_str, "utf-8")
print t

prints

u'Gaga\u2019s'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号