python-3.x - 将句子中的表情符号转换为单词或文本
问题描述
我有这样的句子:“这个地方真棒\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'
解决方案
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')
推荐阅读
- tensorflow - 如何使用本地可解释模型无关的解释(石灰)进行多标签 GRU 回归
- kdb - KDB 仅获取仅存在于一张表中的行
- spring - 如何在春季启动中的 OAuth2AuthenticationProcessingFilter 末尾添加自定义安全过滤器?
- c# - 启动中的服务注入
- c# - 当我点击它时,如何使文本框不可编辑?
- prometheus - 当我看到指标值增加时,为什么 increase() 只显示零值?
- php - 如何在 PHP(PDO) 中显示 dbms_output.put_line
- discord.py - 如何让我的机器人留在语音聊天中,直到它被告知离开
- node.js - cypress imap 阅读电子邮件 - 最佳实践
- reactjs - 为什么当我通过函数调用 axios 时它没有更新?