-2

How can i remove all special characters like "!@#$%^&*()_+" and similar things in python except dot (.), alphanumeric and space in python?

Yuda Prawira
  • 10,249
  • 7
  • 43
  • 51
  • Related: http://stackoverflow.com/questions/3939361/remove-specific-characters-from-a-string-in-python, http://stackoverflow.com/q/875968/102937 – Robert Harvey May 21 '13 at 17:43
  • 5
    Your question is a little vague. What is the set of "non-special" characters? Non-alpha characters? – Jerome May 21 '13 at 17:43
  • 1
    "All characters are special" -- @tchrist – tripleee May 21 '13 at 17:47
  • related: http://stackoverflow.com/q/265960/4279 http://stackoverflow.com/q/11066400/4279 – jfs May 21 '13 at 17:48
  • 2
    `output_string = ''.join(c for c in input_string if c.isalnum() or c in '. ')` This means: concatenate all characters from the input string which are alphanumeric or part of the string ". " (dot, space). – Florian Rhiem May 21 '13 at 17:51
  • it's so lame when not giving a time to edit and closed this and downvote this. @Florian Rhiem thanks – Yuda Prawira May 21 '13 at 17:52

1 Answers1

1

you can use maketrans/translate to translate each character you don't want to some special character then use replace to replace that character with the empty string.

the other way would just be to go though character by character and only concatenate each character on to some new string if it is a desired character.

Jake
  • 717
  • 5
  • 19
  • @Gunslinger_ Stack Overflow isn't here to do the work for you; Jake has supplied an answer explaining how to do it, and now it's your job to implement it. – FThompson May 21 '13 at 17:51