简体   繁体   English

从 string.punctuation 中删除标点符号

[英]Removing punctuation from string.punctuation

I would like to know how to remove some punctuation symbol from the following list我想知道如何从以下列表中删除一些标点符号

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

Specifically, I would like to remove @?&#!^_ to use it here:具体来说,我想删除@?&#!^_以在此处使用它:

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

Thank you in advance for your answers and advice.预先感谢您的回答和建议。

You can use re.sub您可以使用re.sub

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

The same way you are removing punctuation from the input string.与从输入字符串中删除标点符号的方式相同。

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

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

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