如何使用Unicodedata模块处理Python 3中的多字符Unicode表情符号?
当我与表情符号合作并尝试使用unicodedata
模块获取其编码点和名称时,我一直在多字符表情符号表情符号。该模块拒绝让我使用字符串,而是想要字符。我尝试了标准化,我尝试在utf-8
和unicode-escape
中编码,然后一次又一次地研究了它,但是我没有成功地找到正在发生的事情!
emojis = ["
While I was working with emojis and attempting to acquire their codepoint and names with the unicodedata
module, I kept having issues with multi-character emojis. The module refuses to let me use strings and instead wanted characters. I tried normalizing, I tried encoding in utf-8
and unicode-escape
, and I researched it again and again, but I was not successful in finding what was going on!
emojis = ["????", "????", "????", "????", "❣️", "✨"]
for emoji in emojis:
codepoint: str = hex(ord(emoji))
filename = 'emoji_u{0}.png'.format(codepoint[2:])
print('{emoji} ({codepoint}) => {filename}'.format(emoji=emoji,
codepoint=codepoint,
filename=filename))
While yes, the above code does not use the unicodedata
module, it shows you what I was having a problem with regardless...
???? (0x1f496) => emoji_u1f496.png
???? (0x1f498) => emoji_u1f498.png
???? (0x1f49d) => emoji_u1f49d.png
???? (0x1f49e) => emoji_u1f49e.png
Traceback (most recent call last):
File "F:/Programming/Languages/Vue.js/lovely/collect.py", line 8, in <module>
codepoint: str = hex(ord(emoji))
TypeError: ord() expected a character, but string of length 2 found
After a break, somehow, I managed to convert the emoji unintentionally, from this: ❣️
to this: ❣
. Python was able to process this new emoji character perfectly fine. The unicodedata
module likes it too!
So what's the difference? Why does one have color and not the other in both my browser and IDE? And most importantly, how do I convert multi-character emojis to single-character emojis in Python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些人感知的单个字符表情符号(称为 graphemes )由多个代码点组成。这是处理它们的方法。我添加了一个复杂的示例:
Some human-perceived single-character emoji (called graphemes) are made up of multiple code points. Here's a way to handle them. I added a complicated example:
Output:
If the emoji are in a single string the rules for processing a single grapheme are complicated, but implemented by the 3rd party
regex
module.\X
matches graphemes:Output: