在Python中将表情符号Unicode转换为文本

6
我有一个用Python(yowsup)编写的应用程�,我在我的机器人中�收文本和表情符�... 我设法将 😀 转�为 "\U0001F600" 代�...
�在我需�将 u"\U0001F600" 转�为 :grinning: 文本或 GRINNING FACE
�这个页�得到了一些�代�... http://www.fileformat.info/info/unicode/char/1F600/index.htm
@signals.message_received.connect
def handle(message):
    #message.log() to see message object properties
    #print(message.log())
    params = {}
    params_upload = {}
    zapsend_host = config.config_variables['zapsend_host']
    zapsend_port = config.config_variables['zapsend_port']

    # CASE TEXT-MESSAGE AND NOT GROUP
    if helper.is_text_message(message.message_entity) and helper.isGroupJid(message.conversation) == False:
        #converted here....
        params['msg']  = message.text.encode('unicode_escape') 
        params['number']  = message.conversation
        params['whatsapp']= config.config_variables['user']
        params['media'] = 'text'
        params['caption'] = ''
        params['name'] = message.who_name
        database_helper.sync_contacts(message.conversation, message.who_name)
        database_helper.save_message_receive(params, message)
        print("MSG FROM CLIENT: "+ message.conversation +" => " + params['msg'])
        requests_helper.request_get(zapsend_host, zapsend_port,'zapsend',params)

知道做什么? - user1767754
我会重新表述我的问题。 - sealabr
你能展示一下你的代码吗? - Jessie
3个回答

20

@sealabr 检查一下这个。

import emoji
print(emoji.demojize('Python is '))
>>Python is :thumbs_up:

2

试试这个:

s = u'\U0001f600'
from emoji.unicode_codes import UNICODE_EMOJI

print UNICODE_EMOJI[s]
>:grinning_face:

这里假设您已经安装了emoji模块。

非常感谢!现在,有什么提示可以用短语来完成这个任务吗?例如:“我有很多表情符号u"\U0001F600"”,然后转换为“我有很多表情符号:grinning_face:”。 - sealabr

2
这种方法可以处理以下两个方面:
  • 表情符号:
  • 表情文本: :-)) ; 8‑D
使用emot库和一些文本操作: 使用flashtext,因为它比常规表达式更快,可以精确查找和/或替换字符串。
from emot.emo_unicode import UNICODE_EMOJI, UNICODE_EMOJI_ALIAS, EMOTICONS_EMO
from flashtext import KeywordProcessor

## formatting
all_emoji_emoticons = {**EMOTICONS_EMO,**UNICODE_EMOJI_ALIAS, **UNICODE_EMOJI_ALIAS}
all_emoji_emoticons = {k:v.replace(":","").replace("_"," ").strip() for k,v in all_emoji_emoticons.items()}

kp_all_emoji_emoticons = KeywordProcessor()
for k,v in all_emoji_emoticons.items():
    kp_all_emoji_emoticons.add_keyword(k, v)
kp_all_emoji_emoticons.replace_keywords('I am an  hehe :-)). Lets try another one . It seems ')

#output==> 'I am an alien hehe Very happy. Lets try another one astonished. It seems ok hand'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接