user13623188
user13623188

Reputation:

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.

Upvotes: 2

Views: 155

Answers (2)

Dishin H Goyani
Dishin H Goyani

Reputation: 7693

You can use re.sub

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

Upvotes: 2

chepner
chepner

Reputation: 531325

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]

Upvotes: 0

Related Questions