skip to Main Content

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


  1. Something like this could probably help 🙂

    def emoji_calback(e):
        '&#x{0};'.format(e.unicode.encode('unicode_escape').decode('utf8').lstrip('\U0u'))
    
    Login or Signup to reply.
  2. You can use something like:

    pyemoji.entities('Hey 😀')
    

    This returns a unicode string which contains the HTML decimal code of the emoji.
    It returns a unicode string like this:

    u'Hey 😀'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search