Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中从字符串中删除表情符号_Python_String_Unicode_Special Characters_Emoji - Fatal编程技术网

在Python中从字符串中删除表情符号

在Python中从字符串中删除表情符号,python,string,unicode,special-characters,emoji,Python,String,Unicode,Special Characters,Emoji,我在Python中找到了用于删除表情符号的代码,但它不起作用。你能帮我修改其他代码吗 我观察到我所有的emjoi都以\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,

我在Python中找到了用于删除表情符号的代码,但它不起作用。你能帮我修改其他代码吗

我观察到我所有的emjoi都以
\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
但这仍然不能移除表情符号并显示它们!有什么线索吗?
因为
[…]
表示一组字符中的任何一个,并且因为一组中由破折号分隔的两个字符表示一个字符范围(通常是“a-z”或“0-9”),所以您的模式表示“斜杠,后跟包含x,{,1,F,6,0,1,范围}到x,{,1,F,6,4,F或}的组中的任何字符”在中间的那个范围是RE调用坏字符范围的方法。在Python 2上,

< p>,你必须使用<代码> u′/Code >文字来创建Unicode字符串。而且,你应该通过<代码> R.Unicode < /Code >标志,并将输入数据转换成Unicode(例如,<代码>文本=数据。解码(“UTF 8”)):

输出
This dog如果您使用的是已接受答案中的示例,但仍然出现“坏字符范围”错误,那么您可能使用的是窄版本(有关详细信息)。重新格式化的regex版本似乎有效:

NON_BMP_RE = re.compile(u"[^\U00000000-\U0000d7ff\U0000e000-\U0000ffff]", flags=re.UNICODE)
NON_BMP_RE.sub(u'', unicode(text, 'utf-8'))

我接受了这个答案,其他人也为我工作了一段时间,但我最终决定将所有字符从中删除。这不包括将来添加到其他Unicode平面(表情符号等存在的地方),这意味着我不必每次添加新的Unicode字符时都更新代码:)

在Python 2.7中,如果文本尚未转换为unicode,请使用下面的负正则表达式(subs anynot在正则表达式中,这是BMP中的所有字符,但用于创建2字节字符的代理项除外)


完成以下步骤的步骤:

import emoji
def give_emoji_free_text(text):
    allchars = [str for str in text.decode('utf-8')]
    emoji_list = [c for c in allchars if c in emoji.UNICODE_EMOJI]
    clean_text = ' '.join([str for str in text.decode('utf-8').split() if not any(i in str for i in emoji_list)])
    return clean_text

尝试了所有答案,不幸的是,他们没有删除新的拥抱脸表情我正在通过@jfs更新我的答案,因为我以前的答案没有考虑其他Unicode标准,如拉丁语、希腊语等。StackOverFlow不允许我删除我以前的答案,因此我正在更新它以匹配最可接受的答案o问题

!/usr/bin/env python
进口稀土
text=u'这是一张笑脸\U0001f602'
打印(文本)#使用表情符号
def deEmojify(文本):
regrex_pattern=re.compile(pattern=“[”
u“\U0001F600-\U0001F64F”#表情符号
u“\U0001F300-\U0001F5FF”#符号和象形文字
u“\U0001F680-\U0001F6FF”#运输和地图符号
u“\U0001F1E0-\U0001F1FF”标志(iOS)
“]+”,标志=re.UNICODE)
返回regrex_pattern.sub(r'',文本)
打印(文本)
这是我以前的回答,不要用这个

defmojify(输入字符串):
返回inputString.encode('ascii','ignore').decode('ascii'))

如果您不喜欢使用regex,最好的解决方案可能是使用

下面是一个返回表情符号自由文本的简单函数(多亏了它):

如果您正在处理包含表情符号的字符串,那么这很简单

# Emojis pattern
emoji_pattern = re.compile("["
                u"\U0001F600-\U0001F64F"  # emoticons
                u"\U0001F300-\U0001F5FF"  # symbols & pictographs
                u"\U0001F680-\U0001F6FF"  # transport & map symbols
                u"\U0001F1E0-\U0001F1FF"  # flags (iOS)
                u"\U00002702-\U000027B0"
                u"\U000024C2-\U0001F251"
                u"\U0001f926-\U0001f937"
                u'\U00010000-\U0010ffff'
                u"\u200d"
                u"\u2640-\u2642"
                u"\u2600-\u2B55"
                u"\u23cf"
                u"\u23e9"
                u"\u231a"
                u"\u3030"
                u"\ufe0f"
    "]+", flags=re.UNICODE)

这是我的解决方案。此解决方案删除了python无法拒绝的其他男性和女性表情符号,将字符串转换为另一个字符集可能会有所帮助:

import emoji
import re


def strip_emoji(text):

    print(emoji.emoji_count(text))

    new_text = re.sub(emoji.get_emoji_regexp(), r"", text)

    return new_text


with open("my_file.md", "r") as file:
    old_text = file.read()

no_emoji_text = strip_emoji(old_text)

with open("file.md", "w+") as new_file:
    new_file.write(no_emoji_text)

亲切问候。

我试图收集unicodes的完整列表。 我用它从推特中提取表情符号,它对我来说非常好

import re
result = re.sub('[(\U0001F600-\U0001F92F|\U0001F300-\U0001F5FF|\U0001F680-\U0001F6FF|\U0001F190-\U0001F1FF|\U00002702-\U000027B0|\U0001F926-\U0001FA9F|\u200d|\u2640-\u2642|\u2600-\u2B55|\u23cf|\u23e9|\u231a|\ufe0f)]+','','A quick brown fox jumps over the lazy dogThis is the easiest code for remove all emoji.

import emoji

def remove_emojis(text: str) -> str:
    return ''.join(c for c in text if c not in emoji.UNICODE_EMOJI)

下面是一个Python 3脚本,它使用表情库的
get\u emoji\u regexp()
——正如kingmakerking和Martijn Pieters在回答/评论中所建议的那样

它从一个文件中读取文本,并将表情符号自由文本写入另一个文件

sent_0 = re.sub('[^A-Za-z0-9]+', ' ', sent_0)

删除表情符号的完整版本

✍ 最好的解决方案是使用外部库。这个库不断更新最新的表情符号,因此可以在任何文本中找到它们。与删除所有unicode字符的ascii解码方法不同,此方法保留这些字符并仅删除表情符号

  • 如果没有,请首先安装表情库:
    pip install emoji
  • 下一步在文件/项目中导入表情符号:
    import emoji
  • 现在要删除所有表情符号,请使用以下语句:
    emoji.get\u emoji\u regexp().sub(“,msg)
    其中msg是要编辑的文本

  • 这就是您所需要的。

    我知道这可能与所问的问题没有直接关系,但它有助于解决从文本中删除表情符号的家长问题。python中有一个名为的模块,它可以非常准确地完成此任务,并删除几乎所有类型的表情符号。它还定期更新,以提供最新的表情符号删除支持。
    用于删除表情符号。使用replace(文本“”)

    对于我来说,在python 3.8中,以下内容用于替换表情符号:

    (?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|[\ud83c\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|[\ud83c\ude32-\ude3a]|[\ud83c\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])
    
    重新导入
    
    这一次(以下以下简称::(((\U0000001F600-\U0000001F600-\U0000001ff0000f3-\U0000001Ff5)杰夫124;\U0001F680-\U0001F680-\U0000001F600-\U0000001f0001f0000F92F-\U00001f0003-\U0001fff5-FFFFF5-FFF杰夫(以下以下??;;;;;;;;;;;;\U0001f4-U0001ff5-U0001ff5-\U0001ffff5-U0001FGGGGGGGGGGGGGGGGGGGGGGGGGfff6-\U0001F6-\U0001F6-\U0001F6-\U0001F6-\U0001F6-\ff删除所有表情符号的代码

    emoji_pattern = re.compile(
        u"(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|[\ud83c\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|[\ud83c\ude32-\ude3a]|[\ud83c\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])"
        "+", flags=re.UNICODE)
    

    我只是使用正则表达式删除了所有特殊字符,这对我来说很有效

    $ pip3 install emoji
    

    对于仍在使用Python 2.7的用户,这可能会有所帮助:

    import emoji
    import re
    
    
    def strip_emoji(text):
    
        print(emoji.emoji_count(text))
    
        new_text = re.sub(emoji.get_emoji_regexp(), r"", text)
    
        return new_text
    
    
    with open("my_file.md", "r") as file:
        old_text = file.read()
    
    no_emoji_text = strip_emoji(old_text)
    
    with open("file.md", "w+") as new_file:
        new_file.write(no_emoji_text)
    
    因此,要在代码中使用它,它将有点像这样:


    现在我们实际上不再使用Python 2.7了,为什么还需要这样做呢?一些系统/Python实现仍然使用Python 2.7,比如Amazon Redshift中的Python UDF。

    我可以通过以下方式摆脱表情符号

    表情符号安装

    导入表情符号
    def remove_表情符号(字符串):
    返回emoji.get_emoji_regexp().sub(u'',字符串)
    
    表情='(`ヘ')表情符号不受限制
    sent_0 = re.sub('[^A-Za-z0-9]+', ' ', sent_0)
    
    (?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|[\ud83c\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|[\ud83c\ude32-\ude3a]|[\ud83c\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])
    
    emoji_pattern = re.compile(
        u"(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|[\ud83c\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|[\ud83c\ude32-\ude3a]|[\ud83c\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])"
        "+", flags=re.UNICODE)
    
    $ pip3 install emoji