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
This is how I would do it:
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)