我需要从字符串中删除所有特殊字符,标点符号和空格,以便我只有字母和数字。


当前回答

较短的方法:

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

如果你想在单词和数字之间有空格,用''代替''

其他回答

下面是一个正则表达式,用于匹配不是字母或数字的字符串:

[^A-Za-z0-9]+

下面是执行正则表达式替换的Python命令:

re.sub('[^A-Za-z0-9]+', '', mystring)
s = re.sub(r"[-()\"#/@;:<>{}`+=~|.!?,]", "", s)
#!/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

你可以添加更多的特殊字符,这将被“意味着什么,即他们将被删除”所取代。

字符串。标点符号包含以下字符:

'!"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~'

可以使用translate和maketrans函数将标点符号映射为空值(替换)

import string

'This, is. A test!'.translate(str.maketrans('', '', string.punctuation))

输出:

'This is A test'
import re
my_string = """Strings are amongst the most popular data types in Python. We can create the strings by enclosing characters in quotes. Python treats single quotes the 

和双引号一样。”""

# if we need to count the word python that ends with or without ',' or '.' at end

count = 0
for i in text:
    if i.endswith("."):
        text[count] = re.sub("^([a-z]+)(.)?$", r"\1", i)
    count += 1
print("The count of Python : ", text.count("python"))