Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing words in text file using a dictionary

I'm trying to open a text file and then read through it replacing certain strings with strings stored in a dictionary.

Based on answers to How do I edit a text file in Python? I could pull out the dictionary values before doing the replacing, but looping through the dictionary seems more efficient.

The code doesn't produce any errors, but also doesn't do any replacing.

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
like image 323
David Manning Avatar asked Mar 31 '17 11:03

David Manning


2 Answers

I used items() to iterate over key and values of your fields dict.

I skip the blank lines with continue and clean the others with rstrip()

I replace every keys found in the line by the values in your fields dict, and I write every lines with 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
like image 199
Kruupös Avatar answered Oct 23 '22 00:10

Kruupös


If you can find a regex pattern covering all your keys, you could use re.sub for a very efficient solution : you only need one pass instead of parsing your whole text for each search term.

In your title, you mention "replacing words". In that case, '\w+' would work just fine.

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))

It outputs :

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

Also, be very careful when modifying a file in place. I'd advice you to write a second file with the replacements. Once you are 100% sure that it works perfectly, you could switch to inplace=True.

like image 21
Eric Duminil Avatar answered Oct 22 '22 23:10

Eric Duminil