Archive

Posts Tagged ‘cleaning’

remove punctuations from a text

February 5, 2017 Leave a comment

Problem
You have a text and you want to remove punctuations from it. Example:

in:
"Hello! It is time to remove punctuations. It is easy, you will see."

out:
"Hello It is time to remove punctuations It is easy you will see"

Solution
Let’s see a Python 3 solution:

>>> import string
>>> tr = str.maketrans("", "", string.punctuation)
>>> s = "Hello! It is time to remove punctuations. It is easy, you will see."
>>> s.translate(tr)
'Hello Its time to remove punctuations Its easy youll see'

Docs: str.maketrans(), str.translate().

Advertisements
Categories: python Tags: , ,