2

I have couple of strings (each string is a set of words) which has special characters in them. I know using strip() function, we can remove all occurrences of only one specific character from any string. Now, I would like to remove set of special characters (include !@#%&*()[]{}/?<> ) etc.

What is the best way you can get these unwanted characters removed from the strings.

in-str = "@John, It's a fantastic #week-end%, How about () you"

out-str = "John, It's a fantastic week-end, How about you"

Dropout
  • 13,653
  • 10
  • 56
  • 109
Kumar
  • 314
  • 3
  • 5
  • 16
  • The ` () ` will be particularly difficult to get rid of without regular expressions. – bozdoz May 13 '13 at 14:49
  • May I ask **why** you want to do this? In particular, if you want to prevent code injection attacks, you might prefer to _escape_ special characters rather than remove them. How that would go would depend on the specific application. – Jasmijn May 13 '13 at 19:44

4 Answers4

2
import string

s = "@John, It's a fantastic #week-end%, How about () you"
for c in "!@#%&*()[]{}/?<>":
    s = string.replace(s, c, "")

print s

prints "John, It's a fantastic week-end, How about you"

Jacek Przemieniecki
  • 5,757
  • 1
  • 12
  • 15
1

The strip function removes only leading and trailing characters. For your purpose I would use python set to store your characters, iterate over your input string and create new string from characters not present in the set. According to other stackoverflow article this should be efficient. At the end, just remove double spaces by clever " ".join(output_string.split()) construction.

char_set = set("!@#%&*()[]{}/?<>")
input_string = "@John, It's a fantastic #week-end%, How about () you"
output_string = ""

for i in range(0, len(input_string)):
    if not input_string[i] in char_set:
        output_string += input_string[i]

output_string = " ".join(output_string.split())
print output_string
Community
  • 1
  • 1
Dale Cooper
  • 310
  • 2
  • 9
1

Try out this:

import re

foo = 'a..!b...c???d;;'
chars = [',', '!', '.', ';', '?']

print re.sub('[%s]' % ''.join(chars), '', foo)

I presume that this is what you wanted.

Dropout
  • 13,653
  • 10
  • 56
  • 109
  • Btw, I recommend building the array of chars which are not accepted by a foreach loop, or in a similar way to ensure dynamic editing of the restricted chars. – Dropout May 13 '13 at 14:43
0

try

s = "@John, It's a fantastic #week-end%, How about () you"
chars = "!@#%&*()[]{}/?<>"
s_no_chars = "".join([k for k in s if k not in chars])
s_no_chars_spaces = " ".join([ d for d in "".join([k for k in s if k not in chars]).split(" ") if d])
gariel
  • 687
  • 3
  • 13