简体   繁体   English

如何在python中解码javascript unicode字符串?

[英]How to decode javascript unicode string in python?

I found that it is a character of TWO HEARTS here . 我发现这是一个性格TWO HEARTS 这里 I tried to decode it: 我试图对其进行解码:

a = '\ud83d\udc95'
a.encode('utf-8').decode('utf-8')
UnicodeEncodeError: 'utf-8' codec can't encode characters in position 0-1: surrogates not allowed

How to get the right symbol? 如何获得正确的符号?

Thank you. 谢谢。

The page you link to tells you the 您链接到的页面告诉您

Python Escape u'\\U0001f495' Python换码u'\\ U0001f495'

Use it: 用它:

>>> u'\U0001f495'
'💕'

Use json module to help you solve different Unicode . 使用json模块可以帮助您解决不同的Unicode

import json

a = '\ud83d\udc95'
m = json.dumps({"k": a})
print(json.loads(m)["k"])  # 💕

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM