去掉特殊字符,只保留中文、英文、数字importrestring="123我123456abcdefgABCVDFF?/,.,.:;:''';'''[]{}()()《》"print(string)123I123456abcdefgABCVDFF?/,.,.:;:''';'''[]{}()()()《》sub_str=re.sub(u"([^\u4e00-\u9fa5\u0030-\u0039\u0041-\u005a\u0061-\u007a])","",string)print(sub_str)result:123I123456abcdefgABCVDFF函数说明sub(pattern,repl,string)将字符串中所有匹配的表达式替换为repl[^**]表示不匹配此字符集中的任何字符\u4e00-\u9fa5汉字的Unicode范围\u0030-\u0039数字的Unicode范围\u0041-\u005a大写字母的Unicode范围\u0061-\u007a小写字母的Unicode范围\uAC00-\uD7AF韩文的unicode范围\u3040-\u31FF日文的unicode范围
