0

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?

2
  • 1
    unicode aside, you have a logic error. in your loop you keep reassigning replaced but you keep using original so the output will end up being the original string with only the last term removed Commented Feb 23, 2022 at 8:10
  • Oh yes. Thanks for pointing that out. Commented Feb 23, 2022 at 8:54

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.