使用字典替换文本文件中的单词

时间:2017-03-31 11:08:19

标签: python python-2.7 dictionary in-place

我试图打开一个文本文件,然后通过它用字典中存储的字符串替换某些字符串。

基于How do I edit a text file in Python?的答案,我可以在替换之前提取字典值,但循环遍历字典似乎更有效。

代码不会产生任何错误,但也不会替换。

import fileinput

text = "sample file.txt"
fields = {"pattern 1": "replacement text 1", "pattern 2": "replacement text 2"}

for line in fileinput.input(text, inplace=True):
    line = line.rstrip()
    for i in fields:
         for field in fields:
             field_value = fields[field]

             if field in line:
                  line = line.replace(field, field_value)


             print line

6 个答案:

答案 0 :(得分:2)

我使用items()来迭代key dict的valuesfields

我跳过continue的空白行,并使用rstrip()

清除其他行

我将keys中的line替换为values dict中的fields,并用print写下每一行。

import fileinput

text = "sample file.txt"
fields = {"pattern 1": "replacement text 1", "pattern 2": "replacement text 2"}


for line in fileinput.input(text, inplace=True):
    line = line.rstrip()
    if not line:
        continue
    for f_key, f_value in fields.items():
        if f_key in line:
            line = line.replace(f_key, f_value)
    print line

答案 1 :(得分:1)

如果你能找到涵盖所有密钥的正则表达式模式,你可以使用re.sub获得一个非常有效的解决方案:你只需要一次传递,而不是为每个搜索词解析整个文本。

在你的标题中,你提到“替换单词”。在这种情况下,'\w+'可以正常工作。

import re

fields = {"pattern 1": "replacement text 1", "pattern 2": "replacement text 2"}

words_to_replace = r'\bpattern \d+\b'

text = """Based on answers to How do I edit a text file in Python? pattern 1 I could pull out
the dictionary values before doing the replacing, but looping through the dictionary seems more efficient.
Test pattern 2
The code doesn't produce any errors, but also doesn't do any replacing. pattern 3"""

def replace_words_using_dict(matchobj):
    key = matchobj.group(0)
    return fields.get(key, key)

print(re.sub(words_to_replace, replace_words_using_dict, text))

输出:

Based on answers to How do I edit a text file in Python? replacement text 1 I could pull out
the dictionary values before doing the replacing, but looping through the dictionary seems more efficient.
Test replacement text 2
The code doesn't produce any errors, but also doesn't do any replacing. pattern 3

另外,在适当修改文件时要非常小心。我建议你用替换件写第二个文件。一旦您100%确定它完美运行,您就可以切换到inplace=True

答案 2 :(得分:0)

import fileinput

text = "sample file.txt"
fields = {"pattern 1": "replacement text 1", "pattern 2": "replacement text 2"}

for line in fileinput.input(text, inplace=True):
    line = line.rstrip()
    for field in fields:
        if field in line:
            line = line.replace(field, fields[field])

    print line

答案 3 :(得分:0)

如果您对Python更熟悉,可以使用官方文档中的提示:

7.1. string — Common string operations

子类模板类,您可以在其中定义每个单独的世界将是新的占位符,然后使用{ {1}}你可以得到一个漂亮可靠的解决方案。

答案 4 :(得分:0)

只是想出了如何通过遍历字典来一次性替换txt文件中许多不同单词的方法(仅匹配整个单词)。 如果我想用“ John”代替“ 1”,但是最终将“ 12”变成“ John2”,那将真的很烦。以下代码对我有用。

import re

match = {}  # create a dictionary of words-to-replace and words-to-replace-with

f = open("filename","r")
data = f.read() # string of all file content

def replace_all(text, dic):
    for i, j in dic.items():
        text = re.sub(r"\b%s\b"%i, j, text) 
        # r"\b%s\b"% enables replacing by whole word matches only
    return text

data = replace_all(data,match)
print(data) # you can copy and paste the result to whatever file you like

答案 5 :(得分:-1)

我就是这样做的:

fields = {"pattern 1": "replacement text 1", "pattern 2": "replacement text 2"}

with open('yourfile.txt', 'w+') as f:
    s = f.read()
    for key in fields:
        s = s.replace(key, fields[key])
    f.write(s)