问题
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
回答1:
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
回答2:
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
.
回答3:
If You are more familiar with Python, You can use tips from Official documentation:
7.1. string — Common string operations
And subclass, the Template class, in which you define somehow that every single world will be a new placeholder, and then with safe_substitute()
You could get a nice and reliable solution.
回答4:
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
回答5:
Just figured out how to replace lots of different words in a txt file at one go, by iterating through a dictionary (whole word matches only). It would be really annoying if I want to replace "1" with "John", but ends up turning "12" into "John2." The following code is what works for me.
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
回答6:
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)
来源:https://stackoverflow.com/questions/43139174/replacing-words-in-text-file-using-a-dictionary