1

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.

  • What's wrong with your code? – Itay Aug 13 '20 at 11:55
  • I would need to remove some puctuation from string.punctuation. So in my code I should consider a limited version of string.punctuation. –  Aug 13 '20 at 12:04
  • 1
    Does this answer your question? [How can I customize what characters are filtered out using string.punctuation?](https://stackoverflow.com/questions/45427439/how-can-i-customize-what-characters-are-filtered-out-using-string-punctuation) – DarrylG Aug 13 '20 at 12:09

2 Answers2

1

You can use re.sub

re.sub("[@?&#!^_]", "", string.punctuation)
'"$%\'()*+,-./:;<=>[\\]`{|}~'
Dishin H Goyani
  • 7,195
  • 3
  • 26
  • 37
0

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]
chepner
  • 497,756
  • 71
  • 530
  • 681