Search code examples
pythonunicodedecode

Unicode unescaping Python


I'm retrieving usernames from an API and I'm getting values like :

\u00e2\u0098\u0085Random Name\u00e2\u0098\u0085 <3

When I try to print it out, I end up getting:

★Random Name★ <3

But it's supposed to be:

★Random Name★ <3

\u00e2\u0098\u0085 seems to be a constructor for and it looks like unicode escape sequences, but clearly something's going wrong in the conversion.
Need some help on how to go about partially unescaping the string.

Edit: (Additional details)
I'm trying to create a discord bot that regularly updates roles based on retrieved player information.

The first value above is exactly what I get from the API and I can't do anything to change that

★Random Name★ <3 is the message being posted in the server.
I'm certain discord supports the character set because I can directly paste the username without issues

data = json.loads(response.text) is used to parse and store the response
await message.channel.send(f"Name: {data['name']}") is used to send the message

send() is a function part of discord.py to send messages in a specific channel


Solution

  • print('\u00e2\u0098\u0085Random Name\u00e2\u0098\u0085 <3'.encode('latin').decode())
    

    result:

    ★Random Name★ <3