如何替换字符串的多个子串?

时间:2011-05-24 21:15:24

标签: python text replace

我想使用.replace函数替换多个字符串。

我目前有

string.replace("condition1", "")

但希望有类似

的内容
string.replace("condition1", "").replace("condition2", "text")

虽然这感觉不是很好的语法

这样做的正确方法是什么?有点像grep / regex,你可以\1\2将字段替换为某些搜索字符串

26 个答案:

答案 0 :(得分:217)

这是一个简短的例子,应该用正则表达式来处理:

import re

rep = {"condition1": "", "condition2": "text"} # define desired replacements here

# use these three lines to do the replacement
rep = dict((re.escape(k), v) for k, v in rep.iteritems()) 
#Python 3 renamed dict.iteritems to dict.items so use rep.items() for latest versions
pattern = re.compile("|".join(rep.keys()))
text = pattern.sub(lambda m: rep[re.escape(m.group(0))], text)

例如:

>>> pattern.sub(lambda m: rep[re.escape(m.group(0))], "(condition1) and --condition2--")
'() and --text--'

答案 1 :(得分:101)

你可以做一个很好的小循环函数。

def replace_all(text, dic):
    for i, j in dic.iteritems():
        text = text.replace(i, j)
    return text

其中text是完整字符串,dic是字典 - 每个定义都是一个字符串,用于替换该字词的匹配。

注意:在Python 3中,iteritems()已替换为items()


小心: Python词典没有可靠的迭代顺序。此解决方案仅在以下情况下解决您的问题:

  • 替换顺序无关紧要
  • 替换可以更改以前的替换结果

例如:

d = { "cat": "dog", "dog": "pig"}
mySentence = "This is my cat and this is my dog."
replace_all(mySentence, d)
print(mySentence)

可能的输出#1:

"This is my pig and this is my pig."

可能的输出#2

"This is my dog and this is my pig."

一种可能的解决方法是使用OrderedDict。

from collections import OrderedDict
def replace_all(text, dic):
    for i, j in dic.items():
        text = text.replace(i, j)
    return text
od = OrderedDict([("cat", "dog"), ("dog", "pig")])
mySentence = "This is my cat and this is my dog."
replace_all(mySentence, od)
print(mySentence)

输出:

"This is my pig and this is my pig."

小心#2:如果您的text字符串太大或字典中有很多对,效率低下。

答案 2 :(得分:81)

以下是使用reduce的第一个解决方案的变体,以备您正常使用。 :)

repls = {'hello' : 'goodbye', 'world' : 'earth'}
s = 'hello, world'
reduce(lambda a, kv: a.replace(*kv), repls.iteritems(), s)

martineau甚至更好的版本:

repls = ('hello', 'goodbye'), ('world', 'earth')
s = 'hello, world'
reduce(lambda a, kv: a.replace(*kv), repls, s)

答案 3 :(得分:65)

为什么不这样的解决方案?

s = "The quick brown fox jumps over the lazy dog"
for r in (("brown", "red"), ("lazy", "quick")):
    s = s.replace(*r)

#output will be:  The quick red fox jumps over the quick dog

答案 4 :(得分:28)

我在F.J.s上建立了这个很好的答案:

import re

def multiple_replacer(*key_values):
    replace_dict = dict(key_values)
    replacement_function = lambda match: replace_dict[match.group(0)]
    pattern = re.compile("|".join([re.escape(k) for k, v in key_values]), re.M)
    return lambda string: pattern.sub(replacement_function, string)

def multiple_replace(string, *key_values):
    return multiple_replacer(*key_values)(string)

一次性使用:

>>> replacements = (u"café", u"tea"), (u"tea", u"café"), (u"like", u"love")
>>> print multiple_replace(u"Do you like café? No, I prefer tea.", *replacements)
Do you love tea? No, I prefer café.

请注意,由于替换仅在一次通过中完成,“café”更改为“tea”,但它不会更改回“café”。

如果您需要多次进行相同的替换,您可以轻松创建替换功能:

>>> my_escaper = multiple_replacer(('"','\\"'), ('\t', '\\t'))
>>> many_many_strings = (u'This text will be escaped by "my_escaper"',
                       u'Does this work?\tYes it does',
                       u'And can we span\nmultiple lines?\t"Yes\twe\tcan!"')
>>> for line in many_many_strings:
...     print my_escaper(line)
... 
This text will be escaped by \"my_escaper\"
Does this work?\tYes it does
And can we span
multiple lines?\t\"Yes\twe\tcan!\"

改进:

  • 将代码转换为函数
  • 添加了多行支持
  • 修复了逃脱中的错误
  • 易于为特定的多次替换创建功能

享受! : - )

答案 5 :(得分:28)

这只是对F.J和MiniQuark的一个更简洁的回顾。实现多个同时串替换所需的全部功能如下:

def multiple_replace(string, rep_dict):
    pattern = re.compile("|".join([re.escape(k) for k in sorted(rep_dict,key=len,reverse=True)]), flags=re.DOTALL)
    return pattern.sub(lambda x: rep_dict[x.group(0)], string)

用法:

>>>multiple_replace("Do you like cafe? No, I prefer tea.", {'cafe':'tea', 'tea':'cafe', 'like':'prefer'})
'Do you prefer tea? No, I prefer cafe.'

如果您愿意,您可以从这个更简单的替换功能开始自己的专用替换功能。

答案 6 :(得分:19)

我想提出字符串模板的用法。只需将要替换的字符串放在字典中即可完成所有设置!来自docs.python.org

的示例
>>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'
>>> d = dict(who='tim')
>>> Template('Give $who $100').substitute(d)
Traceback (most recent call last):
[...]
ValueError: Invalid placeholder in string: line 1, col 10
>>> Template('$who likes $what').substitute(d)
Traceback (most recent call last):
[...]
KeyError: 'what'
>>> Template('$who likes $what').safe_substitute(d)
'tim likes $what'

答案 7 :(得分:10)

就我而言,我需要用名称简单替换唯一键,所以我想到了这一点:

a = 'This is a test string.'
b = {'i': 'I', 's': 'S'}
for x,y in b.items():
    a = a.replace(x, y)
>>> a
'ThIS IS a teSt StrIng.'

答案 8 :(得分:7)

这是我的0.02美元。它基于Andrew Clark的答案,只是更清楚一点,它还涵盖了当要替换的字符串是要替换的另一个字符串的子字符串(更长的字符串获胜)时的情况

def multireplace(string, replacements):
    """
    Given a string and a replacement map, it returns the replaced string.

    :param str string: string to execute replacements on
    :param dict replacements: replacement dictionary {value to find: value to replace}
    :rtype: str

    """
    # Place longer ones first to keep shorter substrings from matching
    # where the longer ones should take place
    # For instance given the replacements {'ab': 'AB', 'abc': 'ABC'} against 
    # the string 'hey abc', it should produce 'hey ABC' and not 'hey ABc'
    substrs = sorted(replacements, key=len, reverse=True)

    # Create a big OR regex that matches any of the substrings to replace
    regexp = re.compile('|'.join(map(re.escape, substrs)))

    # For each match, look up the new string in the replacements
    return regexp.sub(lambda match: replacements[match.group(0)], string)

如果您有任何提案,请随时修改此this gist

答案 9 :(得分:5)

我需要一个解决方案,其中要替换的字符串可以是正则表达式, 例如,通过用一个空格替换多个空格字符来帮助规范化长文本。基于其他人的答案,包括MiniQuark和mmj,这就是我想出的:

def multiple_replace(string, reps, re_flags = 0):
    """ Transforms string, replacing keys from re_str_dict with values.
    reps: dictionary, or list of key-value pairs (to enforce ordering;
          earlier items have higher priority).
          Keys are used as regular expressions.
    re_flags: interpretation of regular expressions, such as re.DOTALL
    """
    if isinstance(reps, dict):
        reps = reps.items()
    pattern = re.compile("|".join("(?P<_%d>%s)" % (i, re_str[0])
                                  for i, re_str in enumerate(reps)),
                         re_flags)
    return pattern.sub(lambda x: reps[int(x.lastgroup[1:])][1], string)

它适用于其他答案中给出的示例,例如:

>>> multiple_replace("(condition1) and --condition2--",
...                  {"condition1": "", "condition2": "text"})
'() and --text--'

>>> multiple_replace('hello, world', {'hello' : 'goodbye', 'world' : 'earth'})
'goodbye, earth'

>>> multiple_replace("Do you like cafe? No, I prefer tea.",
...                  {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'})
'Do you prefer tea? No, I prefer cafe.'

对我来说最重要的是你也可以使用正则表达式,例如仅替换整个单词,或者规范化空格:

>>> s = "I don't want to change this name:\n  Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace(s, re_str_dict)
"You don't want to change this name: Philip II of Spain"

如果要将字典键用作普通字符串, 你可以在使用例如多个这个功能:

def escape_keys(d):
    """ transform dictionary d by applying re.escape to the keys """
    return dict((re.escape(k), v) for k, v in d.items())

>>> multiple_replace(s, escape_keys(re_str_dict))
"I don't want to change this name:\n  Philip II of Spain"

以下函数可以帮助您在字典键中找到错误的正则表达式(因为来自multiple_replace的错误消息并非常有用):

def check_re_list(re_list):
    """ Checks if each regular expression in list is well-formed. """
    for i, e in enumerate(re_list):
        try:
            re.compile(e)
        except (TypeError, re.error):
            print("Invalid regular expression string "
                  "at position {}: '{}'".format(i, e))

>>> check_re_list(re_str_dict.keys())

请注意,它不会链接替换,而是同时执行它们。这样可以在不限制其功能的情况下提高效率。为了模仿链接的影响,您可能只需要添加更多字符串替换对并确保对的预期排序:

>>> multiple_replace("button", {"but": "mut", "mutton": "lamb"})
'mutton'
>>> multiple_replace("button", [("button", "lamb"),
...                             ("but", "mut"), ("mutton", "lamb")])
'lamb'

答案 10 :(得分:2)

我建议代码应为: 例子。

z = "My name is Ahmed, and I like coding "
print(z.replace(" Ahmed", " Dauda"). replace(" like", " Love" ))

它将按要求打印出所有更改。

答案 11 :(得分:2)

Python 3.8开始并引入assignment expressions (PEP 572):=运算符),我们可以在列表理解中应用替换项:

# text = "The quick brown fox jumps over the lazy dog"
# replacements = [("brown", "red"), ("lazy", "quick")]
[text := text.replace(a, b) for a, b in replacements]
# text = 'The quick red fox jumps over the quick dog'

答案 12 :(得分:1)

要仅替换一个字符,请使用translate,而str.maketrans是我最喜欢的方法。

tl; dr> result_string = your_string.translate(str.maketrans(dict_mapping))


演示

my_string = 'This is a test string.'
dict_mapping = {'i': 's', 's': 'S'}
result_good = my_string.translate(str.maketrans(dict_mapping))
result_bad = my_string
for x, y in dict_mapping.items():
    result_bad = result_bad.replace(x, y)
print(result_good)  # ThsS sS a teSt Strsng.
print(result_bad)   # ThSS SS a teSt StrSng.

答案 13 :(得分:1)

我也在努力解决这个问题。在进行很多替换的情况下,正则表达式很费力,比循环string.replace慢四倍(在我的实验条件下)。

您绝对应该尝试使用 Flashtext 库(blog post hereGithub here)。 In my case对于每个文档,它的速度要比快两个数量级,从1.8 s到0.015 s(正则表达式花费7.7 s)

在上面的链接中很容易找到使用示例,但这是一个有效的示例:

    from flashtext import KeywordProcessor
    self.processor = KeywordProcessor(case_sensitive=False)
    for k, v in self.my_dict.items():
        self.processor.add_keyword(k, v)
    new_string = self.processor.replace_keywords(string)

请注意,Flashtext会一次性进行替换(以避免 a-> b b-> c 将'a'转换为'c')。 Flashtext还会查找整个单词(因此'is'与'th is '不匹配)。如果您的目标是几个单词(用“你好”代替“这是”),则效果很好。

答案 14 :(得分:1)

这是一个样本,它对长字符串更有效,并且有许多小的替代品。

source = "Here is foo, it does moo!"

replacements = {
    'is': 'was', # replace 'is' with 'was'
    'does': 'did',
    '!': '?'
}

def replace(source, replacements):
    finder = re.compile("|".join(re.escape(k) for k in replacements.keys())) # matches every string we want replaced
    result = []
    pos = 0
    while True:
        match = finder.search(source, pos)
        if match:
            # cut off the part up until match
            result.append(source[pos : match.start()])
            # cut off the matched part and replace it in place
            result.append(replacements[source[match.start() : match.end()]])
            pos = match.end()
        else:
            # the rest after the last match
            result.append(source[pos:])
            break
    return "".join(result)

print replace(source, replacements)

关键在于避免很多长串的连接。我们将源字符串剪切为片段,在构成列表时替换一些片段,然后将整个片段连接回字符串。

答案 15 :(得分:1)

我不知道速度,但这是我的工作日快速修复:

reduce(lambda a, b: a.replace(*b)
    , [('o','W'), ('t','X')] #iterable of pairs: (oldval, newval)
    , 'tomato' #The string from which to replace values
    )

...但我喜欢上面的#1正则表达式答案。注意 - 如果一个新值是另一个值的子字符串,则该操作不可交换。

答案 16 :(得分:1)

你真的不应该这样做,但我觉得它太酷了:

>>> replacements = {'cond1':'text1', 'cond2':'text2'}
>>> cmd = 'answer = s'
>>> for k,v in replacements.iteritems():
>>>     cmd += ".replace(%s, %s)" %(k,v)
>>> exec(cmd)

现在,answer是所有替换的结果

再次,这是非常 hacky,并不是你应该定期使用的东西。但是如果你需要的话,知道你可以做这样的事情真是太好了。

答案 17 :(得分:1)

您可以使用pandas库和replace函数,该函数支持完全匹配以及正则表达式替换。例如:

df = pd.DataFrame({'text': ['Billy is going to visit Rome in November', 'I was born in 10/10/2010', 'I will be there at 20:00']})

to_replace=['Billy','Rome','January|February|March|April|May|June|July|August|September|October|November|December', '\d{2}:\d{2}', '\d{2}/\d{2}/\d{4}']
replace_with=['name','city','month','time', 'date']

print(df.text.replace(to_replace, replace_with, regex=True))

修改后的文本是:

0    name is going to visit city in month
1                      I was born in date
2                 I will be there at time

您可以找到示例here。注意,文本上的替换是按照它们在列表中出现的顺序

答案 18 :(得分:0)

我今天遇到了类似的问题,我不得不多次使用 .replace() 方法,但我感觉不太好。所以我做了这样的事情:

REPLACEMENTS = {'<': '&lt;', '>': '&gt;', '&': '&amp;'}

event_title = ''.join([REPLACEMENTS.get(c,c) for c in event['summary']])

答案 19 :(得分:0)

我觉得这个问题需要单行递归lambda函数答案才能完整,仅因为如此。这样:

>>> mrep = lambda s, d: s if not d else mrep(s.replace(*d.popitem()), d)

用法:

>>> mrep('abcabc', {'a': '1', 'c': '2'})
'1b21b2'

注意:

  • 这将消耗输入字典。
  • Python字典从3.6开始保留输入顺序;其他答案中的相应警告不再适用。为了向后兼容,可以求助于基于元组的版本:
>>> mrep = lambda s, d: s if not d else mrep(s.replace(*d.pop()), d)
>>> mrep('abcabc', [('a', '1'), ('c', '2')])

注意:与python中的所有递归函数一样,太大的递归深度(即太大的替换字典)将导致错误。参见例如here

答案 20 :(得分:0)

我的方法是首先对字符串进行标记,然后为每个标记决定是否包含它。

如果我们可以假设哈希映射/集合的 O(1) 查找,可能会更高效:

remove_words = {"we", "this"}
target_sent = "we should modify this string"
target_sent_words = target_sent.split()
filtered_sent = " ".join(list(filter(lambda word: word not in remove_words, target_sent_words)))

filtered_sent 现在是 'should modify string'

答案 21 :(得分:0)

另一个例子: 输入列表

error_list = ['[br]', '[ex]', 'Something']
words = ['how', 'much[ex]', 'is[br]', 'the', 'fish[br]', 'noSomething', 'really']

所需的输出是

words = ['how', 'much', 'is', 'the', 'fish', 'no', 'really']

代码:

[n[0][0] if len(n[0]) else n[1] for n in [[[w.replace(e,"") for e in error_list if e in w],w] for w in words]] 

答案 22 :(得分:0)

这是我解决问题的方法。我在聊天机器人中使用它来一次替换不同的单词。

def mass_replace(text, dct):
    new_string = ""
    old_string = text
    while len(old_string) > 0:
        s = ""
        sk = ""
        for k in dct.keys():
            if old_string.startswith(k):
                s = dct[k]
                sk = k
        if s:
            new_string+=s
            old_string = old_string[len(sk):]
        else:
            new_string+=old_string[0]
            old_string = old_string[1:]
    return new_string

print mass_replace("The dog hunts the cat", {"dog":"cat", "cat":"dog"})

这将成为The cat hunts the dog

答案 23 :(得分:0)

从安德鲁的宝贵答案开始,我开发了一个脚本,从文件中加载字典,并详细说明打开的文件夹中的所有文件以进行替换。该脚本从外部文件加载映射,您可以在其中设置分隔符。我是初学者,但我发现这个脚本在多个文件中进行多次替换时非常有用。它在几秒钟内加载了一个包含1000多个条目的字典。它不优雅,但它对我有用

import glob
import re

mapfile = input("Enter map file name with extension eg. codifica.txt: ")
sep = input("Enter map file column separator eg. |: ")
mask = input("Enter search mask with extension eg. 2010*txt for all files to be processed: ")
suff = input("Enter suffix with extension eg. _NEW.txt for newly generated files: ")

rep = {} # creation of empy dictionary

with open(mapfile) as temprep: # loading of definitions in the dictionary using input file, separator is prompted
    for line in temprep:
        (key, val) = line.strip('\n').split(sep)
        rep[key] = val

for filename in glob.iglob(mask): # recursion on all the files with the mask prompted

    with open (filename, "r") as textfile: # load each file in the variable text
        text = textfile.read()

        # start replacement
        #rep = dict((re.escape(k), v) for k, v in rep.items()) commented to enable the use in the mapping of re reserved characters
        pattern = re.compile("|".join(rep.keys()))
        text = pattern.sub(lambda m: rep[m.group(0)], text)

        #write of te output files with the prompted suffice
        target = open(filename[:-4]+"_NEW.txt", "w")
        target.write(text)
        target.close()

答案 24 :(得分:0)

以下是使用字典进行此操作的另一种方法:

listA="The cat jumped over the house".split()
modify = {word:word for number,word in enumerate(listA)}
modify["cat"],modify["jumped"]="dog","walked"
print " ".join(modify[x] for x in listA)

答案 25 :(得分:0)

或者只是为了快速入侵:

for line in to_read:
    read_buffer = line              
    stripped_buffer1 = read_buffer.replace("term1", " ")
    stripped_buffer2 = stripped_buffer1.replace("term2", " ")
    write_to_file = to_write.write(stripped_buffer2)