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?
you can do this:
# 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 = original
for term in terms:
replaced = replaced.replace(term, u'something else')
print(replaced)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.