6

I have some strings that I want to delete some unwanted characters from them. For example: Adam'sApple ----> AdamsApple.(case insensitive) Can someone help me, I need the fastest way to do it, cause I have a couple of millions of records that have to be polished. Thanks

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
Hossein
  • 40,161
  • 57
  • 141
  • 175

9 Answers9

6

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
Pescolly
  • 922
  • 11
  • 18
  • 1
    It was before I got into regular expressions, basically the code equivalent of my embarassing goth phase. Although, it does allow the untrained to make modifications, which is pretty much a necessity at my work. – Pescolly Sep 21 '16 at 19:24
6

One simple way:

>>> s = "Adam'sApple"
>>> x = s.replace("'", "")
>>> print x
'AdamsApple'

... or take a look at regex substitutions.

miku
  • 181,842
  • 47
  • 306
  • 310
5

Any characters in the 2nd argument of the translate method are deleted:

>>> "Adam's Apple!".translate(None,"'!")
'Adams Apple'

NOTE: translate requires Python 2.6 or later to use None for the first argument, which otherwise must be a translation string of length 256. string.maketrans('','') can be used in place of None for pre-2.6 versions.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • I might be helpful to explicitly mention `string.maketrans('', '')` as a substitute for `None` for Python < 2.6 – jfs May 06 '10 at 14:26
  • Six times faster than `"".join(char for char in text if char not in bad_chars)` :) – badp May 06 '10 at 16:22
2

Try:

"Adam'sApple".replace("'", '')

One step further, to replace multiple characters with nothing:

import re
print re.sub(r'''['"x]''', '', '''a'"xb''')

Yields:

ab
dlamotte
  • 6,145
  • 4
  • 31
  • 40
1
str.replace("'","");
Delan Azabani
  • 79,602
  • 28
  • 170
  • 210
1

As has been pointed out several times now, you have to either use replace or regular expressions (most likely you don't need regexes though), but if you also have to make sure that the resulting string is plain ASCII (doesn't contain funky characters like é, ò, µ, æ or φ), you could finally do

>>> u'(like é, ò, µ, æ or φ)'.encode('ascii', 'ignore')
'(like , , ,  or )'
Esteban Küber
  • 36,388
  • 15
  • 79
  • 97
0

An alternative that will take in a string and an array of unwanted chars

    # function that removes unwanted signs from str
    #Pass the string to the function and an array ofunwanted chars

def removeSigns(str,arrayOfChars):

    charFound = False

    newstr = ""

    for letter in str:
        for char in arrayOfChars:
            if letter == char:
                charFound = True
                break
        if charFound == False:
            newstr += letter
        charFound = False

    return newstr
0

Let's say we have the following list:

states = [' Alabama ', 'Georgia!', 'Georgia', 'georgia', 'south carolina##', 'West virginia?']

Now we will define a function clean_strings()

import re

def clean_strings(strings):
    result = []
    for value in strings:
        value = value.strip()
        value = re.sub('[!#?]', '', value)
        value = value.title()
        result.append(value)
    return result

When we call the function clean_strings(states)

The result will look like:

['Alabama',
'Georgia',
'Georgia',
'Georgia',
'Florida',
'South Carolina',
'West Virginia']
Prasang Singhal
  • 121
  • 1
  • 2
0

I am probably late for the answer but i think below code would also do ( to an extreme end) it will remove all the unncesary chars:

a = '; niraj kale 984wywn on 2/2/2017'
a= re.sub('[^a-zA-Z0-9.?]',' ',a)
a = a.replace('  ',' ').lstrip().rstrip()

which will give

'niraj kale 984wywn on 2 2 2017'