I have a django application that consumes the twitter public API.
The tweets the application received contains some emojis and I want to convert it into HTML decimal equivalent.
Searching for python emoji
I found two libraries (emoji_unicode
, pyemoji
).
I am using the two libraries as following to get the decimal value of the emoji included in tweet body;
import emoji_unicode, pyemoji
def emoji_callback(e):
t = pyemoji.encode(e.unicode).replace('\u','')
return "&#%s;" % str(int(t, 16))
emoji_unicode.replace(u'Time to ⛽ ',emoji_callback)
The previous example works fine but for some other emojis it did not work and it throws an invalid literal for int() with base 16
exception. For example the below code does not work.
emoji_unicode.replace(u'Time to 🌄',call)
Questions
1- Is there a simpler way to get the HTML decimal of the emoji in the tweet body instead of what is implemented here?
2- If no, how could I solve that exception and get the code working for all emojis?
2
Answers
Something like this could probably help 🙂
You can use something like:
This returns a unicode
string
which contains theHTML
decimal code of the emoji.It returns a unicode string like this: