Removing unwanted characters from a string in Python

前端 未结 9 1873
暖寄归人
暖寄归人 2021-01-18 09:06

I have some strings that I want to delete some unwanted characters from them. For example: Adam\'sApple ----> AdamsApple.(case insensitive) Can someone help

相关标签:
9条回答
  • 2021-01-18 09:47

    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 )'
    
    0 讨论(0)
  • 2021-01-18 09:49

    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 讨论(0)
  • 2021-01-18 09:51

    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
    
    0 讨论(0)
  • 2021-01-18 09:51
    str.replace("'","");
    
    0 讨论(0)
  • 2021-01-18 09:52

    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'

    0 讨论(0)
  • 2021-01-18 09:55

    One simple way:

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

    ... or take a look at regex substitutions.

    0 讨论(0)
提交回复
热议问题