Removing unnecessary characters from a string in Python

I have some lines that I want to remove some unwanted characters from them. For example: Adam'sApple ----> AdamsApple(case insensitive) Can someone help me, I need the fastest way to do this, because I have several million records that need to be polished. Thanks

+4
source share
9 answers

One easy way:

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

... or take a look at regular expression substitutions .

+5
source

All characters of the second argument to the translate method are deleted:

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

: Python 2.6 None , 256. string.maketrans ('', '') None 2.6.

+5

Try:

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

, :

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

:

ab
+2

, ascii, "&". "". , , , .

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
+2
str.replace("'","");
+1

, replace, ( , ), , ASCII ( , é, ò, μ, æ & phi;),

>>> u'(like é, ò, µ, æ or φ)'.encode('ascii', 'ignore')
'(like , , ,  or )'
+1

,

    # 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

, :

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

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

clean_strings(states)

:

['Alabama',
'Georgia',
'Georgia',
'Georgia',
'Florida',
'South Carolina',
'West Virginia']
0

, , , , ( ), :

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 2 2 2017'

0
source

Source: https://habr.com/ru/post/1744329/


All Articles