简体   繁体   English

如何在不连接单词的情况下从 Python 中的字符串中删除除空格之外的所有非字母字符

[英]How remove all non-alphabet chars excluding white space from string in Python without joining the words

I have a variable named para and I want to remove all the non-alphabet characters excluding whitespace characters.我有一个名为 para 的变量,我想删除除空白字符之外的所有非字母字符。 For the following input:对于以下输入:

para = "I a, going #?5 1throu$gh Lots Of ]pain
        kcb H in"

required output所需的输出

para =  "I a going through Lots Of pain
        kcb H in"

Code Tried代码尝试

import re
regex = re.compile('[^a-zA-Z]')
regex.sub('', para)

Output getting输出获取

'IagoingthroughLotsOfpain'
import re
regex = re.compile('[^a-zA-Z\s]')
regex.sub('', para)

\\s matches any whitespace character (equivalent to [\\r\\n\\t\\f\\v ] ). \\s匹配任何空白字符(相当于[\\r\\n\\t\\f\\v ] )。 See regex101.com .请参阅regex101.com

Chack it破解它

import re
pattern = re.compile('[\W_0-9]+')
para =  '''I a going through Lots Of pain
            kcb H in'''
dirty_list = para.split()
clean_list = [pattern.sub('', word) for word in dirty_list]
clean_string = ' '.join(clean_list)
print(clean_string)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM