# 从string中删除所有特殊字符，标点符号和空格

### 10 Solutions collect form web for “从string中删除所有特殊字符，标点符号和空格”

` `>>> string = "Special \$#! characters spaces 888323" >>> ''.join(e for e in string if e.isalnum()) 'Specialcharactersspaces888323'` `

` `S.isalnum() -> bool Return True if all characters in S are alphanumeric and there is at least one character in S, False otherwise.` `

` `[^A-Za-z0-9]+` `

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

` `import re cleanString = re.sub('\W+','', string )` `

` `#!/usr/bin/python import re strs = "how much for the maple syrup? \$20.99? That's ricidulous!!!" print strs nstr = re.sub(r'[?|\$|.|!]',r'',strs) print nstr nestr = re.sub(r'[^a-zA-Z0-9 ]',r'',nstr) print nestr` `

` `In [20]: filter(str.isalnum, 'string with special chars like !,#\$% etcs.') Out[20]: 'stringwithspecialcharslikeetcs'` `

• `string1 = 'Special \$#! characters spaces 888323'`
• `string2 = 'how much for the maple syrup? \$20.99? That s ricidulous!!!'`

### 例1

`'.join(e for e in string if e.isalnum())`

• `string1` – 结果：10.7061979771
• `string2` – 结果：7.78372597694

### 例2

`import re re.sub('[^A-Za-z0-9]+', '', string)`

• `string1` – 结果：7.10785102844
• `string2` – 结果：4.12814903259

### 例3

`import re re.sub('\W+','', string)`

• `string1` – 结果：3.11899876595
• `string2` – 结果：2.78014397621

` `import unicodedata # strip of crap characters (based on the Unicode database # categorization: # http://www.sql-und-xml.de/unicode-database/#kategorien PRINTABLE = set(('Lu', 'Ll', 'Nd', 'Zs')) def filter_non_printable(s): result = [] ws_last = False for c in s: c = unicodedata.category(c) in PRINTABLE and c or u'#' result.append(c) return u''.join(result).replace(u'#', u' ')` `

` `>>> import re >>> rx = re.compile(u'[\W_]+', re.UNICODE) >>> data = u''.join(unichr(i) for i in range(256)) >>> rx.sub(u'', data) u'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\xaa\xb2 [snip] \xfe\xff' >>>` `

` `import string def clean(instr): return instr.translate(None, string.punctuation + ' ')` `

` `import re abc = "askhnl#\$%askdjalsdk" ddd = abc.replace("#\$%","") print (ddd)` `