Skip to main content
 首页 » 编程设计

python之如何从字符串中删除所有标点符号

2025年05月04日19dudu

用于从字符串 x 中删除所有标点符号。 我想使用 re.findall(),但我一直在努力知道要在 中写什么...... 我知道我可以通过写作得到所有标点符号:

import string 
y = string.punctuation 

但是如果我写:

re.findall(y,x)  

它说:

 raise error("multiple repeat") 
 sre_constants.error: multiple repeat 

谁能解释一下我们应该在 re.findall 函数中写些什么?

请您参考如下方法:

为此您甚至可能不需要 RegEx。你可以像这样简单地使用translate

import string 
print data.translate(None, string.punctuation)