Python 2.7 Python2.7删除特殊的chac,空格,但不删除中文字符
结果应为:“特殊字符空间88323kek郭" 我已经试过了Python 2.7 Python2.7删除特殊的chac,空格,但不删除中文字符,python-2.7,Python 2.7,结果应为:“特殊字符空间88323kek郭" 我已经试过了 print'。使用和函数连接(c代表字符串中的c。如果u'\u4e00'解决方案,则解码('utf-8'): string = "Special $#! characters spaces 888323 Kek ཌི ༜ 郭 ༜ དྀ " 输出: import re string = "Special $#! characters spaces 888323 Kek ཌི ༜ 郭 ༜ དྀ " # de
print'。使用和函数连接(c代表字符串中的c。如果u'\u4e00'解决方案,则解码('utf-8'):
string = "Special $#! characters spaces 888323 Kek ཌི ༜ 郭 ༜ དྀ "
输出:
import re
string = "Special $#! characters spaces 888323 Kek ཌི ༜ 郭 ༜ དྀ "
# defining the pattern which should match all characters excepting alphanumeric and chinese
pattern = re.compile(u'[^a-z0-9⺀-⺙⺛-⻳⼀-⿕々〇〡-〩〸-〺〻㐀-䶵一-鿃豈-鶴侮-頻並-龎]', re.UNICODE | re.IGNORECASE)
result = pattern.sub('', string)
# print(result) Python v.3 printing
print result
我不想删除像“$!?”@RomanPerekhrestexception special chac like!@$%^&*():“/。@ChinYe,根据您的“异常列表”'special$\!CharactersPaces88323k显示预期结果如何郭' 像这样,只需将这些字符添加到pattern character类中,它将适用于您的新案例:pattern=re.compile(u'[^a-z0-9!@$%^&*():“/。⺀-⺙⺛-⻳⼀-⿕々〇〡-〩〸-〺〻㐀-䶵一-鿃豈-鶴侮-頻並-龎]', re.UNICODE | re.IGNORECASE)
Specialcharactersspaces888323Kek郭