【问题标题】:Dictionary replaces substrings Python 2.7字典替换子字符串 Python 2.7
【发布时间】:2018-07-10 19:11:18
【问题描述】:

我想在新文本文件中替换文本文件中的数字。我试图用函数 Dictionary 来解决它,但现在 python 也替换了子字符串。

例如:我想将数字 014189 替换为 1489,使用此代码它还将 014896 替换为 1489 - 我该如何摆脱这个?谢谢!!!

replacements = {'01489':'1489', '01450':'1450'}
infile = open('test_fplan.txt', 'r')
outfile = open('test_fplan_neu.txt', 'w')


for line in infile:
    for src, target in replacements.iteritems():
        line = line.replace(src, target)
    outfile.write(line)

【问题讨论】:

  • 看起来问题可能包含一些可能导致混淆的错字。当你写 014189 时,应该只是 01489 吗?此外,代码是否真的将 014896 替换为 1489,或者更确切地说,替换为 14896? (我认为它不会删除 6)。

标签: python replace substring


【解决方案1】:

我不知道您的输入文件看起来如何,但如果数字被空格包围,这应该可以:

replacements = {' 01489 ':' 1489 ', ' 01450 ':' 1450 '}

【讨论】:

  • 非常感谢您的快速答复!帮了大忙!
【解决方案2】:

看起来您担心的是它还会修改包含您的 src 模式作为子字符串的数字。为避免这种情况,您需要首先定义应遵守的界限。例如,您是否要坚持只替换被空格包围的匹配数字?或者也许只是没有相邻的数字(或句点或逗号)。由于您可能希望使用正则表达式来限制匹配,正如 JoshuaF 在另一个答案中所建议的那样,您可能需要避免使用简单的替换函数来支持 re 库中的某些内容。

【讨论】:

    【解决方案3】:

    使用正则表达式和否定的外观:

    import re
    
    replacements = {'01489':'1489', '01450':'1450'}
    
    def find_replacement(match_obj):
        number = match_obj.group(0)
        return replacements.get(number, number)
    
    with open('test_fplan.txt') as infile:
        with open('test_fplan_neu.txt', 'w') as outfile:
            outfile.writelines(
                 re.sub(r'(?<!\d)(\d+)(?!\d)', find_replacement, line)
                 for line in infile
            )
    

    【讨论】:

      【解决方案4】:

      查看正则表达式语法https://docs.python.org/2/library/re.html。它应该允许您准确匹配您正在寻找的任何模式。

      【讨论】: