user47467
user47467

Reputation: 1093

Python replace string in text file with value from list

My problem is to replace strings in a text file, with another string. These key strings are in a list called word_list. I've tried the following, nothing seems to work. It prints out the sentence in document.text as it appears, with no replacement:

  word_list = {'hi' : 'test', 'how' : 'teddy'} 

  with open("document.txt") as main:
      words = main.read().split()

   replaced = []
   for y in words:
         replacement = word_list.get(y, y)
         replaced.append(replacement)
   text = ' '.join(word_list.get(y, y) for y in words)


   print text

   new_main = open("done.txt", 'w')
   new_main.write(text)
   new_main.close()

Content of document.txt:

   hi you, how is he?

Current output is the same as document.txt when it should be:

   test you, teddy is he?

Any solutions/ help would be appreciated :)

Upvotes: 0

Views: 7415

Answers (2)

panda-34
panda-34

Reputation: 4209

As you seem to want to replace words, this will use a more natural definition of 'word':

import re
word_list = {'hi' : 'test', 'how' : 'teddy'}
with open('document.txt') as main, open('done.txt', 'w') as done:
    text = main.read()
    done.write(re.sub(r'\b\w+\b', lambda x: word_list.get(x.group(), x.group()), text))

Upvotes: 2

Christian Witts
Christian Witts

Reputation: 11585

word_list = {'hi' : 'test', 'how' : 'teddy'} 

with open("document.txt") as main:
    with open('done.txt', 'w') as new_main:
        input_data = main.read()
        for key, value in word_list.iteritems():
            input_data = input_data.replace(key, value)

        new_main.write(input_data)

This will read the entire contents of the file (not the most efficient if it's a large file), then iterate over your search and replace items in your dictionary, and call replace on the input text. Once complete it will write the data out to your new file.

Some things to remember with this approach

  • if your input file is large, it will be slow
  • you search pattern can also match word fragments, ie. hi will watch which, so you should cater for that too.

Upvotes: 0

Related Questions