I have a text file containing a list of Unicode strings. Let's say list.txt and I have another dictionary file dict.txt that contains a list of words (Unicode also), which needs to be searched in the first file and replaced with something else. However, my code is executing without error but not doing the find/replace properly.
list.txt
राम गोपाल
राम प्रसाद
etc.
dict.txt
गोपाल
प्रसाद
find_replace.py
import string
# read the dictionary file of terms (each term in one line)
terms = open('dict.txt', encoding='utf-8').read().splitlines()
# read the file that contains terms to be replaced
original = open('list.txt', encoding='utf-8').read()
# initialize
replaced = ""
for term in terms:
replaced = original.replace(term, u"")
print(replaced)
Any suggestion on how to do this?
replacedbut you keep usingoriginalso the output will end up being the original string with only the lasttermremoved