A better way to rewrite multiple appended replace

2019-02-20 06:42发布

问题:

I have a really ugly command where I use many appended "replace()" methods to replace/substitute/scrub many different strings from an original string. For example:

newString = originalString.replace(' ', '').replace("\n", '').replace('()', '').replace('(Deployed)', '').replace('(BeingAssembled)', '').replace('ilo_', '').replace('ip_', '').replace('_ilop', '').replace('_ip', '').replace('backupnetwork', '').replace('_ilo', '').replace('prod-', '').replace('ilo-','').replace('(EndofLife)', '').replace('lctcvp0033-dup,', '').replace('newx-', '').replace('-ilo', '').replace('-prod', '').replace('na,', '')

As you can see, it's a very ugly statement and makes it very difficult to know what strings are in the long command. It also makes it hard to reuse.

What I'd like to do is define an input array of of many replacement pairs, where a replacement pair looks like [<ORIGINAL_SUBSTRING>, <NEW_SUBSTRING>]; where the greater array looks something like:

replacementArray = [
                     [<ORIGINAL_SUBSTRING>, <NEW_SUBSTRING>],
                     [<ORIGINAL_SUBSTRING>, <NEW_SUBSTRING>],
                     [<ORIGINAL_SUBSTRING>, <NEW_SUBSTRING>],
                     [<ORIGINAL_SUBSTRING>, <NEW_SUBSTRING>]
]

AND, I'd like to pass that replacementArray, along with the original string that needs to be scrubbed to a function that has a structure something like:

def replaceAllSubStrings(originalString, replacementArray):
    newString = ''
    for each pair in replacementArray:
        perform the substitution
    return newString

MY QUESTION IS: What is the right way to write the function's code block to apply each pair in the replacementArray? Should I be using the "replace()" method? The "sub()" method? I'm confused as to how to restructure the original code into a nice clean function.

Thanks, in advance, for any help you can offer.

回答1:

You have the right idea. Use sequence unpacking to iterate each pair of values:

def replaceAllSubStrings(originalString, replacementArray):
    for in_rep, out_rep in replacementArray:
        originalString = originalString.replace(in_rep, out_rep)
    return originalString


回答2:

How about using re?

import re

def make_xlat(*args, **kwds):  
    adict = dict(*args, **kwds)  
    rx = re.compile('|'.join(map(re.escape, adict)))  
    def one_xlat(match):  
        return adict[match.group(0)]  
    def xlat(text):  
        return rx.sub(one_xlat, text)  
    return xlat

replaces = {
    "a": "b",
    "well": "hello"
}

replacer = make_xlat(replaces)
replacer("a well?")
# b hello?

You can add as many items in replaces as you want.