I have this code:
def remove_punctuation(self,text):
exclude = set(string.punctuation)
a=''.join(ch for ch in text if ch not in exclude)
return ''.join(c for c in a if not ud.category(c).startswith('P'))
First I would like to know what this does :
ch for ch in text if ch not in exclude
How is it possible to write a for loop like that?
second, I want to replace those punctuation let's say in a text like this : "hello_there?my_friend!" with a space using the above code. How can I change that code to do that?
The piece of code:
a = ''.join([ch for ch in text if ch not in exclude])
is equivalent to
string_without_punctuation = ''
exclude = set(string.punctuation) # =set('!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~')
for character in text:
if character not in exclude:
string_without_punctuation += character
You could simply do this to replace the punctuation with spaces:
string_without_punctuation = ''
exclude = set(string.punctuation) # =set('!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~')
for character in text:
if character not in exclude:
string_without_punctuation += character
else:
string_without_punctuation += ' '