I need to read in a text file, replace selected words and write the changed text back out to a file. Is there an easy way with Python?

I need to read in a text file, replace selected words and write the changed text back out to a file. Is there an easy way with Python?

there are several ways.
1) reading with for loop

o = open("output","a") #open for append
for line in open("file"):
   line = line.replace("someword","newword")
   o.write(line + "\n") 
o.close()

2) with while loop

f = open("file")
o = open("output","a")
while 1:
  line = f.readline()
  if not line: break
  line = line.replace("someword","newword")
  o.write(line + "\n")
o.close()

3) using regular expression and reading the whole file into memory

import re
o = open("output","w")
data = open("file").read()
o.write( re.sub("someword","newword",data)  )
o.close()

If you don't want to explicitly declare file handles for writing to output file, you can use fileinput module, to modify file in place

import fileinput
for line in fileinput.FileInput("file",inplace=1):
   line = line.replace("blah","blahblah")
   print line
commented: It helped me out quickly when needed +0

Thanks ghostdog!
I will have to play with those code ideas. I need to replace several different words in the text file.

Here is an example, where the target and replacement words are in a dictionary. The Python module re is used to do the gruntwork ...

# read a text file, replace multiple words specified in a dictionary
# write the modified text back to a file
 
import re
 
def replace_words(text, word_dic):
    """
    take a text and replace words that match a key in a dictionary with
    the associated value, return the changed text
    """
    rc = re.compile('|'.join(map(re.escape, word_dic)))
    def translate(match):
        return word_dic[match.group(0)]
    return rc.sub(translate, text)
 
 
str1 = \
"""When we see a Space Shuttle sitting on its launch pad, there are two big
booster rockets attached to the sides of the main fuel tank. These are solid
rocket boosters, made by Thiokol at their factory in Utah. The engineers who
designed the solid rocket boosters might have preferred to make them a bit
fatter, but they had to be shipped by train from the factory to the launch
site.  The railroad line from the factory runs through a tunnel in the
mountains.  The boosters had to fit through that tunnel.  The tunnel is
slightly wider than the railroad track.  The width of the railroad track
came from the width of horse-drawn wagons in England, which were as wide
as two horses' behinds.  So, a major design feature of what is the world's
most advanced transportation system was determined over two thousand years
ago by the width of a horse's ass!
"""
 
test_file = "Mword1.txt"
# create a test file for this example
fout = open(test_file, "w")
fout.write(str1)
fout.close()
 
# read the file
fin = open(test_file, "r")
str2 = fin.read()
fin.close()
 
# the dictionary has target_word:replacement_word pairs
word_dic = {
'booster': 'rooster',
'rocket': 'pocket',
'solid': 'salted',
'tunnel': 'funnel',
'ship': 'slip'}
 
# call the function and get the changed text
str3 = replace_words(str2, word_dic)
 
# test
print str3
 
# write changed text back out
fout = open("Mword2.txt", "w")
fout.write(str3)
fout.close()

Thanks again, the last code example will work for me. I will have to read up on the module re to digest it all.

If i wanted to read the words to be replaced from another text file and then replace them wat modifications to the code would i have to make??

Thanks for the tips ..
As a note .. These work great for smaller files, but not so good for very large files (such as over 100mb)

Any samples for handling large files?

ignore

Hi Vegaseat,

Commenting - Here is an example, where the target and replacement words are in a dictionary. The Python module re is used to do the gruntwork ...

This code works perfectly. However, do you or anyone else know, if I want to mark everywhere a change is made with an expression - how to do this rather than using the dictionary.

What I mean is:

input text: "this is a fat cat
that is a large pan
that is a thick slice of bread"

Oupput: "this is a thin cat
that is a large pan
that is a thin slice of bread"

Desired output: "//Modification
this is a thin cat
that is a large pan
//Modification
that is a thin slice of bread"

Something like this (did not read the original code of this thread though, next time start your own thread and include link to old thread if you like)?

input_text = """this is a fat cat
that is a large pan
that is a thick slice of bread"""

targets, new, output = ['fat','thick'], 'thin', ''
for line in input_text.splitlines():
    for target in targets:
        if target in line:
            output +="//Modification\n"
            line = line.replace(target,new)
    output += line+'\n'
print output

ignore

ignore

Hi tonyjv,

Thank you very much for your reply. It works! :)

I actually needed to change quite a lot of words in a text file.

So lets say:

'fat - thin'
'thick - slim'

and so on.

is there a way to modify line 5 in your code to do that? I can't seem to figure out.

since I would like to change quite a few words and/or expressions it would be ideal to use something like that vegaseat has used:

word_dic = {
'booster': 'rooster',
'rocket': 'pocket',
'solid': 'salted',
'tunnel': 'funnel',
'ship': 'slip'}

Any idea would be very much appreciated :)

what's about this code:

f=open(file_name,'r').read()
m=f.replace(old_word,new_word)
print m

?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.