首页 > 解决方案 > 将句子中的表情符号转换为单词或文本

问题描述

我有这样的句子:“这个地方真棒\xF0\x9F\x98\x89”。我想用相应的文本替换 \xF0\x9F\x98\x89 。因此结果就像“这个地方真棒,眨眼的微笑。”。我正在使用python 3。我想我可以使用表情符号包进行演示,但这需要表情符号而不是“\xF0\x9F\x98\x89”。

mystr = "OMG the place is Awesome !!!!!!!!!!!! \xf0\x9f\x98\x9dl"
mystr = mystr.decode('utf-8')
print(emoji.demojize(mystr))

为此我得到错误:AttributeError:'str'对象没有属性'decode'

标签: python-3.xemoji

解决方案


ByteStrings start with a b notation.

>>> mystr = "OMG the place is Awesome !!!!!!!!!!!! \xf0\x9f\x98\x9dl"
>>> mystr = mystr.decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'decode'
>>> mystr = b"OMG the place is Awesome !!!!!!!!!!!! \xf0\x9f\x98\x9dl"
>>> mystr = mystr.decode('utf-8')

推荐阅读