从 string.punctuation 中删除标点符号

Removing punctuation from string.punctuation

我想知道如何从以下列表中删除一些标点符号

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

具体来说,我想删除 @?&#!^_ 以在此处使用它:

def pr(text):


#1 Remove Punctuationa
nopunc = [char for char in text if char not in string.punctuation]
nopunc = ''.join(nopunc)

#2 Remove Stop Words
clean = [word for word in nopunc.split() if word.lower() not in stopwords.words('english')]

return clean

提前感谢您的回答和建议。

与从输入字符串中删除标点符号的方法相同。

limited_punc = [char for char in string.punctuation if char in "@?&#!^_"]
nopunc = [char for char in text if char not in limited_punc]

您可以使用re.sub

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