繁体   English   中英

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

[英]Replacing words in text file using a dictionary

我正在尝试打开一个文本文件,然后读取它用字典中存储的字符串替换某些字符串。

基于如何在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

我使用items()迭代你的fields dict的keyvalues

我跳过空白行continue并用rstrip()清理其他rstrip()

我用fields dict中的values替换line找到的每个keys ,然后用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

如果你能找到一个涵盖所有密钥的正则表达式模式,你可以使用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

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

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

7.1。 string - 常用字符串操作

子类Template类,在其中定义每个单独的世界将成为一个新的占位符 ,然后使用safe_substitute()您可以获得一个漂亮可靠的解决方案。

刚刚弄清楚如何通过遍历字典(仅限整个单词匹配)一次性替换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

我就是这样做的:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM