One simple way:
>>> s = "Adam'sApple"
>>> x = s.replace("'", "")
>>> print x
'AdamsApple'
... or take a look at regex substitutions.
Answer from miku on Stack Overflowparsing - Removing unwanted characters from a string in Python - Stack Overflow
How do I remove remove stuff like (. , ' *) from a string?
Python - remove unwanted characters from a string - Stack Overflow
remove all possible unwanted characters from python string at once - Stack Overflow
One simple way:
>>> s = "Adam'sApple"
>>> x = s.replace("'", "")
>>> print x
'AdamsApple'
... or take a look at regex substitutions.
Here is a function that removes all the irritating ascii characters, the only exception is "&" which is replaced with "and". I use it to police a filesystem and ensure that all of the files adhere to the file naming scheme I insist everyone uses.
def cleanString(incomingString):
newstring = incomingString
newstring = newstring.replace("!","")
newstring = newstring.replace("@","")
newstring = newstring.replace("#","")
newstring = newstring.replace("$","")
newstring = newstring.replace("%","")
newstring = newstring.replace("^","")
newstring = newstring.replace("&","and")
newstring = newstring.replace("*","")
newstring = newstring.replace("(","")
newstring = newstring.replace(")","")
newstring = newstring.replace("+","")
newstring = newstring.replace("=","")
newstring = newstring.replace("?","")
newstring = newstring.replace("\'","")
newstring = newstring.replace("\"","")
newstring = newstring.replace("{","")
newstring = newstring.replace("}","")
newstring = newstring.replace("[","")
newstring = newstring.replace("]","")
newstring = newstring.replace("<","")
newstring = newstring.replace(">","")
newstring = newstring.replace("~","")
newstring = newstring.replace("`","")
newstring = newstring.replace(":","")
newstring = newstring.replace(";","")
newstring = newstring.replace("|","")
newstring = newstring.replace("\\","")
newstring = newstring.replace("/","")
return newstring
I have a string:
"Bob 'hit' a ball, the hit BALL flew far after it was hit."
And I want to replace the word 'hit' by 'sample'.
How do I do that when the word gets immediately followed/preceded by stuff like (. , ' *)
Edit: I know using replace() will work but I don't know what special characters there will be in the string. Some strings had characters like ( - [ / + .
This is not regex as you haven't asked it for before
''.join([i if ((i == " " )or (ord(i) < 128 and ord(i) >46)) else '' for i in '“Projected Set-tled Balan&ce†456$'])
Updated for regex
re.sub(r'[^A-Za-z0-9\s]+','', '“Projected Set-tled Balan&ce†456$')
aString.encode('ascii', 'ignore')
My bad, that was pretty dumb of me
Do that but one letter at a time and if you get a error, replace that char with an empty string.
This was asked a lot, but here's these.
How to remove nonAscii characters in python
Replace non-ASCII characters with a single space