Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove punctuation in python?

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?

like image 740
ctrlaltdel Avatar asked Dec 07 '18 07:12

ctrlaltdel


People also ask

How do I remove punctuation from a dataset in Python?

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'] .

How do you remove special and punctuation characters in Python?

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.

How do you remove a comma in Python?

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 .


1 Answers

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'
like image 58
Vlad Bezden Avatar answered Sep 30 '22 10:09

Vlad Bezden