【发布时间】:2015-02-03 02:12:47
【问题描述】:
我希望能够检测文本中的表情符号并查找他们的名字。
我没有使用 unicodedata 模块,我怀疑我不是 了解 UTF-8 约定。
我猜我需要将我的文档加载为 utf-8,然后将 unicode“字符串”分解为 unicode 符号。遍历这些并查找它们。
#new example loaded using pandas and encoding UTF-8
'A man tried to get into my car\U0001f648'
type(test) = unicode
import unicodedata as uni
uni.name(test[0])
Out[89]: 'LATIN CAPITAL LETTER A'
uni.name(test[-3])
Out[90]: 'LATIN SMALL LETTER R'
uni.name(test[-1])
ValueError Traceback (most recent call last)
<ipython-input-105-417c561246c2> in <module>()
----> 1 uni.name(test[-1])
ValueError: no such name
# just to be clear
uni.name(u'\U0001f648')
ValueError: no such name
我通过 google 查找了 unicode 符号,它是一个合法符号。 也许 unicodedata 模块不是很全面......?
我正在考虑从here 创建自己的查找表。 对其他想法感兴趣……这个似乎可行。
【问题讨论】:
-
该字符串不包含您认为的内容。试试打印吧。
-
是的,我没有在这里寻找表情符号,我只是抓住了一些东西......但我会
-
我的意思是,它不是 Unicode 字符串。这是一个字节字符串,看起来像是包含一些Mojibake。
unicodedata如果你给它喂垃圾,它就不会工作。 -
mojibake..好的,就是这样......再次感谢..我会用更好的例子更新......我还必须首先避免使用mojibake..
-
unicodedata可能没有一些最近添加的字符的记录。你可能只需要在它周围加上一个try/except。