簡體   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