I want to remove characters in a string in python:
string.replace(',', '').replace("!", '').replace(":", '').replace(";", '')...
But I have many characters I have to remove. I thought about a list
list = [',', '!', '.', ';'...]
But how can I use the list
to replace the characters in the string
?
You can use str.translate()
:
s.translate(None, ",!.;")
Example:
>>> s = "asjo,fdjk;djaso,oio!kod.kjods;dkps"
>>> s.translate(None, ",!.;")
'asjofdjkdjasooiokodkjodsdkps'
Python 3, single line list comprehension implementation.
from string import ascii_lowercase # 'abcdefghijklmnopqrstuvwxyz'
def remove_chars(input_string, removable):
return ''.join([_ for _ in input_string if _ not in removable])
print(remove_chars(input_string="Stack Overflow", removable=ascii_lowercase))
>>> 'S O'
Here is a more_itertools
approach:
import more_itertools as mit
s = "A.B!C?D_E@F#"
blacklist = ".!?_@#"
"".join(mit.flatten(mit.split_at(s, pred=lambda x: x in set(blacklist))))
# 'ABCDEF'
Here we split upon items found in the blacklist
, flatten the results and join the string.
Also an interesting topic on removal UTF-8 accent form a string converting char to their standard non-accentuated char:
What is the best way to remove accents in a python unicode string?
code extract from the topic:
import unicodedata
def remove_accents(input_str):
nkfd_form = unicodedata.normalize('NFKD', input_str)
return u"".join([c for c in nkfd_form if not unicodedata.combining(c)])
If you are using python3 and looking for the translate
solution - the function was changed and now takes 1 parameter instead of 2.
That parameter is a table (can be dictionary) where each key is the Unicode ordinal (int) of the character to find and the value is the replacement (can be either a Unicode ordinal or a string to map the key to).
Here is a usage example:
>>> list = [',', '!', '.', ';']
>>> s = "This is, my! str,ing."
>>> s.translate({ord(x): '' for x in list})
'This is my string'
Another approach using regex:
''.join(re.split(r'[.;!?,]', s))
You can use the translate method.
s.translate(None, '!.;,')
you could use something like this
def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
This code is not my own and comes from here its a great article and dicusses in depth doing this
''.join(c for c in myString if not c in badTokens)
Remove *%,&@! from below string:
s = "this is my string, and i will * remove * these ** %% "
new_string = s.translate(s.maketrans('','','*%,&@!'))
print(new_string)
# output: this is my string and i will remove these
simple way,
import re
str = 'this is string ! >><< (foo---> bar) @-tuna-# sandwich-%-is-$-* good'
// condense multiple empty spaces into 1
str = ' '.join(str.split()
// replace empty space with dash
str = str.replace(" ","-")
// take out any char that matches regex
str = re.sub('[!@#$%^&*()_+<>]', '', str)
output:
this-is-string--foo----bar--tuna---sandwich--is---good
i think this is simple enough and will do!
list = [",",",","!",";",":"] #the list goes on.....
theString = "dlkaj;lkdjf'adklfaj;lsd'fa'dfj;alkdjf" #is an example string;
newString="" #the unwanted character free string
for i in range(len(TheString)):
if theString[i] in list:
newString += "" #concatenate an empty string.
else:
newString += theString[i]
this is one way to do it. But if you are tired of keeping a list of characters that you want to remove, you can actually do it by using the order number of the strings you iterate through. the order number is the ascii value of that character. the ascii number for 0 as a char is 48 and the ascii number for lower case z is 122 so:
theString = "lkdsjf;alkd8a'asdjf;lkaheoialkdjf;ad"
newString = ""
for i in range(len(theString)):
if ord(theString[i]) < 48 or ord(theString[i]) > 122: #ord() => ascii num.
newString += ""
else:
newString += theString[i]
I am thinking about a solution for this. First I would make the string input as a list. Then I would replace the items of list. Then through using join command, I will return list as a string. The code can be like this:
def the_replacer(text):
test = []
for m in range(len(text)):
test.append(text[m])
if test[m]==','\
or test[m]=='!'\
or test[m]=='.'\
or test[m]=='\''\
or test[m]==';':
#....
test[n]=''
return ''.join(test)
This would remove anything from the string. What do you think about that?
Perhaps a more modern and functional way to achieve what you wish:
>>> subj = 'A.B!C?'
>>> list = set([',', '!', '.', ';', '?'])
>>> filter(lambda x: x not in list, subj)
'ABC'
please note that for this particular purpose it's quite an overkill, but once you need more complex conditions, filter comes handy
Why not a simple loop?
for i in replace_list:
string = string.replace(i, '')
Also, avoid naming lists 'list'. It overrides the built-in function list
.
These days I am diving into scheme, and now I think am good at recursing and eval. HAHAHA. Just share some new ways:
first ,eval it
print eval('string%s' % (''.join(['.replace("%s","")'%i for i in replace_list])))
second , recurse it
def repn(string,replace_list):
if replace_list==[]:
return string
else:
return repn(string.replace(replace_list.pop(),""),replace_list)
print repn(string,replace_list)
Hey ,don't downvote. I am just want to share some new idea.
How about this - a one liner.
reduce(lambda x,y : x.replace(y,"") ,[',', '!', '.', ';'],";Test , , !Stri!ng ..")
Source: Stackoverflow.com