I want to delete unwanted characters in dictionary like:$&@“”’’!?,#%*=…
I just want number ,alphabet and underline.
CodePudding user response:
If you want to sanitize a string so that it has all special characters removed, try this function:
def sanitize(string):
outstr = ""
for char in string:
if char.isalpha() or char.isdigit() or char == "_":
outstr = char
return outstr
CodePudding user response:
you could do a lambda
that returns True
if the text isn't in punctuation symbols (that you don't want), except for '_'
that we will remove with .replace("_","")
, and then using the filter
function to apply that to a list
, then to join
with ''
:
from string import punctuation
string_to_parse = "a$ ehl! th!s i$ "
parse=lambda letter:letter not in punctuation.replace("_",'')
stringcompleted = ''.join(filter(parse,string_to_parse))
print(stringcompleted)
output:
'a ehl ths i '
You could wrap this into a function, like this:
>>> def parses(string_to_parse):
... parse=lambda letter:letter not in __import__("string").punctuation.replace("_",'')
... return ''.join(filter(parse,string_to_parse))
...
>>> parses("s/) (=i;~q8cze%]*'y:|,f")
'siq8czeyf'
but this uses dynamic importing, witch from my test, results in double the time of the other so i suggest normal importing:
>>> def parses(string_to_parse):
... from string import punctuation
... parse=lambda letter:letter not in punctuation.replace("_",'')
... return ''.join(filter(parse,string_to_parse))
>>> parses("s/) (=i;~q8cze%]*'y:|,f")
'siq8czeyf'