我有这个示例字符串:happy t00 go 129.129
,我想只保留空格和字母。到目前为止,我能够提出的非常有效的是:
print(re.sub("\d", "", 'happy t00 go 129.129'.replace('.', '')))
但它仅针对我的示例字符串。如何删除除字母和空格以外的所有字符?
答案 0 :(得分:13)
whitelist = set('abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ')
myStr = "happy t00 go 129.129$%^&*("
answer = ''.join(filter(whitelist.__contains__, myStr))
输出:
>>> answer
'happy t go '
答案 1 :(得分:9)
使用set complement:
re.sub(r'[^a-zA-Z ]+', '', 'happy t00 go 129.129')
答案 2 :(得分:3)
inspectorG4dget的方法略有不同 - 从<h2>Active Forms</h2>
<form action="formsubmit.php" method="post">
<b>Input Text</b>
<input type="text" size="90" name="inputfield" value="" title="The title attribute works like a tool-tip" />
</form>
&amp;生成器理解:
string
(我让myStr更长一点并预编译正则表达式以使事情变得更有趣)
from string import ascii_letters
allowed = set(ascii_letters + ' ')
myStr = 'happy t00 go 129.129'
answer = ''.join(l for l in myStr if l in allowed)
answer
# >>> 'happy t go '
每回路53μs±6.43μs(平均值±标准偏差,7次运行,每次10000次循环)
每回路43.3μs±7.48μs(平均值±标准偏差,7次运行,每次10000次循环)
每个循环26μs±509 ns(平均值±标准偏差,7次运行,每次10000次循环)