Does anybody know how to modify this script so that it iterates through the key values each time it finds a specific word
word_replacement = {'rat':['pat', 'grey', 'squeeky']}
with open("main.txt") as main:
words = main.read().split()
replaced = []
for y in words:
replacement = word_replacement.get(y, y)
replaced.append(replacement)
text = ' '.join(replaced)
print (text)
new_main = open("main.txt", 'w')
new_main.write(text)
new_main.close()
sample input:
rat went to the rat park to play with rat friends at the rat party
desired output:
pat went to the grey park to play with squeeky friends at the pat party