Supernova
Supernova

Reputation: 63

Removing Punctuation and Replacing it with Whitespace using Replace in Python

trying to remove the following punctuation in python I need to use the replace methods to remove these punctuation characters and replace it with whitespace ,.:;'"-?!/

here is my code:

text_punct_removed = raw_text.replace(".", "")
text_punct_removed = raw_text.replace("!", "")
print("\ntext with punctuation characters removed:\n", text_punct_removed)

It only will remove the last one I try to replace, so I tried combining them

text_punct_removed = raw_text.replace(".", "" , "!", "")
print("\ntext with punctuation characters removed:\n", text_punct_removed)

but I get an error message, how do I remove multiple punctuation? Also there will be an issue if I put the " in quotes like this """ which will make a comment, is there a way around that? thanks

Upvotes: 1

Views: 9882

Answers (5)

Down the Stream
Down the Stream

Reputation: 697

This works in Python3.5.3:

from string import punctuation

raw_text_with_punctuations = "text, with: punctuation; characters? all over ,.:;'\"-?!/"
print(raw_text_with_punctuations)
for char in punctuation:
    raw_text_with_punctuations = raw_text_with_punctuations.replace(char, '')
print(raw_text_with_punctuations)

Upvotes: 1

jnvance
jnvance

Reputation: 41

If you don't need to explicitly use replace:

exclude = set(",.:;'\"-?!/")
text = "".join([(ch if ch not in exclude else " ") for ch in text])

Upvotes: 4

rafaels88
rafaels88

Reputation: 839

If you need to replace all punctuations with space, you can use the built-in punctuation list to replace the string:

Python 3

import string
import re

my_string = "(I hope...this works!)"

translator = re.compile('[%s]' % re.escape(string.punctuation))
translator.sub(' ', my_string)

print(my_string)
# Result:
#  I hope   this works

After, if you want to remove double spaces inside string, you can make:

my_string = re.sub(' +',' ', my_string).strip()

print(my_string)
# Result:
# I hope this works

Upvotes: 1

Fallen
Fallen

Reputation: 4565

Here's a naive but working solution:

for sp in '.,"':
    raw_text = raw_text.replace(sp, '')

Upvotes: 1

DYZ
DYZ

Reputation: 57033

Either remove one character at a time:

raw_text.replace(".", "").replace("!", "")

Or, better, use regular expressions (re.sub()):

re.sub(r"\.|!", "", raw_text)

Upvotes: 0

Related Questions