I've a problem:
E.x. I have a sentence
s = "AAA? BBB. CCC!"
So, I do:
import string
table = str.maketrans('', '', string.punctuation)
s = [w.translate(table) for w in s]
And it's all right. My new sentence will be:
s = "AAA BBB CCC"
But, if I have input sentence like:
s = "AAA? BBB. CCC! DDD.EEE"
after remove punctuation the same method as below I'll have
s = "AAA BBB CCC DDDEEE"
but need:
s = "AAA BBB CCC DDD EEE"
Is any ideas/methods how to solve this problem?
To remove punctuation with Python Pandas, we can use the DataFrame's str. replace method. We call replace with a regex string that matches all punctuation characters and replace them with empty strings. replace returns a new DataFrame column and we assign that to df['text'] .
Using 'str. replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.
Remove Commas From String Using the re Package in Python In the re pacakge of Python, we have sub() method, which can also be used to remove the commas from a string. It replaces all the , in the string my_string with "" and removes all the commas in the string my_string .
string.punctuation contains following characters:
'!"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~'
You can use translate and maketrans functions to map punctuations to empty values (replace)
import string
'AAA? BBB. CCC! DDD.EEE'.translate(str.maketrans('', '', string.punctuation))
Output:
'AAA BBB CCC DDDEEE'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With