我有一个推特集合,我想检查其中的表情符号,但看起来集合的编写过程将所有表情符号转换为字符串,例如 '😊' 在文本中是 ':-)' 和 '😃'是':D'等等所有表情符号。如果我们尝试检查它们的 unicode 代码点,我们会得到 ':-)'.encode('utf-8')
等于 b':-)'
时间 '😊'.encode('utf-8')
等于 'b'\xf0\x9f\x98\x8a
并且相等性检查失败。使用 utf-16
: ':-)'.encode('utf-16')
等于 b'\xff\xfe:\x00-\x00 )\x00'
和 '😊'.encode('utf-16')
是 b'\xff\xfe=\xd8\n\xde'
.那么有什么方法可以将文本表示形式(例如“:-)”转换回表情符号“😊”。
最佳答案
使用 dictionary将任何文本表情符号转换回表情符号,例如如下:
>>> dict_emo = { ':-)' : b'\xf0\x9f\x98\x8a',
... ':)' : b'\xf0\x9f\x98\x8a',
... '=)' : b'\xf0\x9f\x98\x8a', # Smile or happy
... ':-D' : b'\xf0\x9f\x98\x83',
... ':D' : b'\xf0\x9f\x98\x83',
... '=D' : b'\xf0\x9f\x98\x83', # Big smile
... '>:-(' : b'\xF0\x9F\x98\xA0',
... '>:-o' : b'\xF0\x9F\x98\xA0' # Angry face
... }
>>> print( dict_emo[':)'].decode('utf-8'))
😊
>>> print( dict_emo['>:-('].decode('utf-8'))
😠
>>> print( dict_emo[':-D'].decode('utf-8'))
😃
>>>
>>>
>>> dict_emot= { ':-)' : b'\xf0\x9f\x98\x8a'.decode('utf-8'),
... ':)' : b'\xf0\x9f\x98\x8a'.decode('utf-8'),
... '=)' : b'\xf0\x9f\x98\x8a'.decode('utf-8'), # Smile or happy
... ':-D' : b'\xf0\x9f\x98\x83'.decode('utf-8'),
... ':D' : b'\xf0\x9f\x98\x83'.decode('utf-8'),
... '=D' : b'\xf0\x9f\x98\x83'.decode('utf-8'), # Big smile
... '>:-(' : b'\xF0\x9F\x98\xA0'.decode('utf-8'),
... '>:-o' : b'\xF0\x9F\x98\xA0'.decode('utf-8') # Angry face
... }
>>> print( dict_emot[':)'] )
😊
>>> print( dict_emot['>:-o'] )
😠
>>> print( dict_emot['=D'] )
😃
>>>
不幸的是,至少还有两个任务:
- Text Smiley Faces and Their Meaning既不稳定也不确定,另见 Common examples of emoticons (Computer Definition)和 List of emoticons - 尽管有一些尝试创建 resource of all the text smileys and emoticons in the world ;
- Natural Language Processing: What is an algorithmic way to find all smileys in a text?以及如何消除(错误的)嵌入 文本表情符号,例如
:-)
smile in:-))
< em>双下巴。
关于python-3.x - 在python中将字符串转换为表情符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44907866/