| | |
Replace words in a file
Thread Solved
![]() |
•
•
Join Date: Apr 2006
Posts: 148
Reputation:
Solved Threads: 39
•
•
•
•
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?
1) reading with for loop
Python Syntax (Toggle Plain Text)
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
Python Syntax (Toggle Plain Text)
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
Python Syntax (Toggle Plain Text)
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
Python Syntax (Toggle Plain Text)
import fileinput for line in fileinput.FileInput("file",inplace=1): line = line.replace("blah","blahblah") print line
Here is an example, where the target and replacement words are in a dictionary. The Python module re is used to do the gruntwork ...
python Syntax (Toggle Plain Text)
# 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()
May 'the Google' be with you!
![]() |
Similar Threads
- writing a censor program from a file help (Python)
- Replace text in a file (Shell Scripting)
- Scan text file to find all words of 4 characters or less (Shell Scripting)
- getting words from .txt file (C++)
Other Threads in the Python Forum
- Previous Thread: Recognize the amount of digits in a string
- Next Thread: Making a scrollbar follow the last line in a list
| Thread Tools | Search this Thread |
abrupt accessdenied advanced ansi anti apache application approximation argv array backend beginner binary builtin calculator change command converter countpasswordentry csv curved dan08 def dictionary edit event file float format function google heads homework inches input jaunty java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric obexftp output parameters parsing path phonebook pointer prime programming py2exe pygame pyopengl python random recursion redirect remote return reverse scrolledtext session software sprite statictext statistics string strings syntax terminal text thread threading time tlapse tuple twoup ubuntu unicode unit urllib urllib2 variable voip wordgame write wxpython






