去除字符串中的特殊字符,空格和数字等,两种方式: ```python import re #W1 re.sub(r'[^A-Za-z0-9]+', ' ', str1) #W2 re.sub('\W+', ' ', str1) ``` 注意:这两种方式会同时去除字符串中的逗号、句号等分割符. 如果需要保留,可以采用如下方式: ```python re.sub(r'[^a-zA-Z0-9,.\'!?]+',' ',text) ``` 去除字符串中的特殊字符,空格和数字等,两种方式:import re #W1 re.sub(r'[^A-Za-z0-9]+', ' ', str1) #W2 re.sub('\W+', ' ', str1)注意:这两种方式会同时去除字符串中的逗号、句号等分割符. 如果需要保留,可以采用如下方式:re.sub(r'[^a-zA-Z0-9,.\'!?]+',' ',text) Last modification:June 12, 2022 © Allow specification reprint Like If you think my article is useful to you, please feel free to appreciate