清洗中文字符串

def clean(line):
    import re
    def _valid_chr(byte_code):
        '''
        0xE2 0xBA 0x80 - 0xE2 0xBB 0xB3      共 115 个
        0xE2 0xBC 0x80 - 0xE2 0xBF 0x95      共 213 个
        0xE3 0x80 0x85 - 0xE3 0x80 0xA9      共 36 个
        0xE3 0x80 0xB8 - 0xE4 0xB6 0xB5      共 7549 个
        0xE4 0xB8 0x80 - 0xEF 0xA9 0xAA      共 44138 个
        0xEF 0xA9 0xB0 - 0xEF 0xAB 0x99      共 105 个
        0xF0 0xA0 0x80 0x80 - 0xF0 0xAF 0xA8 0x9D        共 64029 个
        '''
        if byte_code >= b'a' and byte_code <= b'z':
            return True
        if byte_code >= b'0' and byte_code <=b'9':
            return True
        if byte_code > b'\xe2\xba\x80' and byte_code < b'\xe2\xbb\xb3':
            return True
        if byte_code > b'\xe2\xbc\x80' and byte_code < b'\xe2\xbf\x95':
            return True
        if byte_code > b'\xe3\x80\x85' and byte_code < b'\xe3\x80\xa9':
            return True
        if byte_code > b'\xe3\x80\xb8' and byte_code < b'\xe4\xb6\xb5':
            return True
        if byte_code >= b'\xe4\xb8\x80' and byte_code < b'\xef\xa9\xaa':
            return True
        if byte_code > b'\xef\xa9\xb0' and byte_code < b'\xef\xab\x99':
            return True
        if byte_code > b'\xf0\xa0\x80\x80' and byte_code < b'\xf0\xaf\xa8\x9d':
            return True
        return False
    SPECIAL_SYM = "[{}]+".format("""\\n\\t!?。"#$%&'()*+-/:;<=>@[\]^_`{|}~⦅⦆「」、、〃《》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿☺😘😔😔😜😜😪😭😰🍶🍧🌄🌅🌊🗻🍚🍣🍦–—‘’‛“”„‟…‧﹏""")
    line = line.lower()
    line = re.sub(SPECIAL_SYM, "", line)
    line = "".join(filter(lambda x: _valid_chr(x.encode('utf8')), line))
    return line

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值