pythonunicodeutfucs2

How to decode javascript unicode string in python?


I found that it is a character of TWO HEARTS here. 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.


Solution

  • Use json module to help you solve different Unicode.

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