字符串出现表情符解决方案(超超超超超级猛)

1.pip install emoji(能不能用各占50%,你要能用,那就是100%)

import emoji

str_ = """放你的表情句子"""
result = emoji.demojize(test_str)
print(emoji.emojize(result))

第三方库,有些站自己做的表情就解决不了

 

2.使用表情符unicode的码表区间,使用re进行替换(概率和第一种一样,你要能用,那就是100%)

import re

def 过滤表情(原字符串, 替换的字符串=''):
    try:
        co = re.compile(u'[\U00010000-\U0010ffff]')
    except re.error:
        co = re.compile(u'[\uD800-\uDBFF][\uDC00-\uDFFF]')
    return re.sub(co,替换的字符串, 原字符串)

原字符串="""放你有表情的字符串"""
print(过滤表情(原字符串))

这个码表范围不知道哪个大佬定义的,现在前端发展的很迅猛,至于表情的码表鬼知道更新多少代了

(用明朝的剑砍清朝的官,你好大的官威啊!)

这就萎了吗,当然不是,如果经常使用qq聊天的就会找到办法了

 

 剩下的就看自己的咯~

 

3.使用r符号

一般经常看到在正则或者文件路径前使用这个 r,同样字符串也可以使用,将字符串变为原始字符串,就能保留表情的原始unicode符号,从而不报错

原字符串 = r"""Join DJ Grant from galaxy107fm New Zealand for a live to air radio and Facebook live interview with the band\n\u27a1\ufe0f \"WILDFIRE\"\n\u2764Kayla and Kelli Lutzwig, a dynamic pop country sister duo from Houston Texas \n\u27a1\ufe0fThursday the 13th of May at 6pm, Houston Texas, USA time\ud83c\uddfa\ud83c\uddf2 or soon after.\n\u27a1\ufe0f Friday the 14th of May at 11am New Zealand \ud83c\uddf3\ud83c\uddff time or soon after\n\nSee you on Facebook live.\nTune in to the live radio broadcast at www.galaxyfm.co.nz"""

print(原字符串)

 

4.使用repr()方法

第3种方法是手动在字符串前加上r,虽然可以解决问题,但是像爬虫或者一些主动的程序中是不会给你机会去加这个 r 的,怎么办???

使用repr()方法

 

 

原字符串 = """Join DJ Grant from galaxy107fm New Zealand for a live to air radio and Facebook live interview with the band\n\u27a1\ufe0f \"WILDFIRE\"\n\u2764Kayla and Kelli Lutzwig, a dynamic pop country sister duo from Houston Texas \n\u27a1\ufe0fThursday the 13th of May at 6pm, Houston Texas, USA time\ud83c\uddfa\ud83c\uddf2 or soon after.\n\u27a1\ufe0f Friday the 14th of May at 11am New Zealand \ud83c\uddf3\ud83c\uddff time or soon after\n\nSee you on Facebook live.\nTune in to the live radio broadcast at www.galaxyfm.co.nz"""

print(repr(原字符串))

 

posted @ 2021-05-14 17:52  黑山老道  阅读(301)  评论(0编辑  收藏  举报