我在 Python 中找到了这段用于删除表情符号的代码,但它不起作用。你能帮忙处理其他代码或解决这个问题吗?
我观察到我所有的 emjois 都以 \xf
开头,但是当我尝试搜索 str.startswith("\xf")
时,出现无效字符错误。
emoji_pattern = r'/[x{1F601}-x{1F64F}]/u'
re.sub(emoji_pattern, '', word)
这是错误:
Traceback (most recent call last):
File "test.py", line 52, in <module>
re.sub(emoji_pattern,'',word)
File "/usr/lib/python2.7/re.py", line 151, in sub
return _compile(pattern, flags).sub(repl, string, count)
File "/usr/lib/python2.7/re.py", line 244, in _compile
raise error, v # invalid expression
sre_constants.error: bad character range
列表中的每一项都可以是一个单词 ['This', 'dog', '\xf0\x9f\x98\x82', 'https://t.co/5N86jYipOI']
更新:我使用了其他代码:
emoji_pattern=re.compile(ur" " " [\U0001F600-\U0001F64F] # emoticons \
|\
[\U0001F300-\U0001F5FF] # symbols & pictographs\
|\
[\U0001F680-\U0001F6FF] # transport & map symbols\
|\
[\U0001F1E0-\U0001F1FF] # flags (iOS)\
" " ", re.VERBOSE)
emoji_pattern.sub('', word)
原文由 Mona Jalal 发布,翻译遵循 CC BY-SA 4.0 许可协议
我正在通过@jfs 更新我对此的回答,因为我之前的回答未能说明其他 Unicode 标准,如拉丁语、希腊语等。StackOverFlow 不允许我删除我之前的回答,因此我正在更新它以匹配最可接受的答案到这个问题。
这是我之前的回答,不要用这个。