I am trying to replace specific words within a text file. I have a text file (test.txt
) the content of the file is as follows:
red red blue
green red blue
red
I wish to replace each instance of red with RED in capitals.
My coding so far is this:
print "What file would you like to read?",
filename = raw_input()
txt = open(filename)
print txt.read()
import re
x=len(re.findall('red', open(filename).read()))
print "The total number of the word 'red' is: %r" % x
I really have no idea how I would go about replacing words and i'm sure my current attempt at just counting the word is bad. I would appreciate any help.