Python ProgrammingPython Programming

How to remove punctuation marks from a string?

Removing Punctuation from text data

During data analysis sometimes punctuation doesn't presents any extra or valuable information. Hence to improve the productivity and effectiveness during data processing below two code snippet will help to remove punctuation from text data.

import re
import string

data = "Stuning even for the non-gamer: This sound track was beautiful!\
It paints the senery in your mind so well I would recomend\
it even to people who hate vid. game music! I have played the game Chrono \
Cross but out of all of the games I have ever played it has the best music! \
It backs away from crude keyboarding and takes a fresher step with grate\
guitars and soulful orchestras.\
It would impress anyone who cares to listen!"

# Methood 1 : Regex
# Remove the special charaters from the read string.
no_specials_string = re.sub('[!#?,.:";]', '', data)
print(no_specials_string)


# Methood 2 : translate()
# Rake translator object
translator = str.maketrans('', '', string.punctuation)
data = data.translate(translator)
print(data)