943,699 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 51959
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 19th, 2007
0

Replace words in a file

Expand Post »
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?
Similar Threads
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Feb 19th, 2007
1

Re: Replace words in a file

Click to Expand / Collapse  Quote originally posted by sneekula ...
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
Python Syntax (Toggle Plain Text)
  1. o = open("output","a") #open for append
  2. for line in open("file"):
  3. line = line.replace("someword","newword")
  4. o.write(line + "\n")
  5. o.close()

2) with while loop
Python Syntax (Toggle Plain Text)
  1. f = open("file")
  2. o = open("output","a")
  3. while 1:
  4. line = f.readline()
  5. if not line: break
  6. line = line.replace("someword","newword")
  7. o.write(line + "\n")
  8. o.close()

3) using regular expression and reading the whole file into memory
Python Syntax (Toggle Plain Text)
  1. import re
  2. o = open("output","w")
  3. data = open("file").read()
  4. o.write( re.sub("someword","newword",data) )
  5. 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)
  1. import fileinput
  2. for line in fileinput.FileInput("file",inplace=1):
  3. line = line.replace("blah","blahblah")
  4. print line
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
Feb 20th, 2007
0

Re: Replace words in a file

Thanks ghostdog!
I will have to play with those code ideas. I need to replace several different words in the text file.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Feb 21st, 2007
0

Re: Replace words in a 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 ...
python Syntax (Toggle Plain Text)
  1. # read a text file, replace multiple words specified in a dictionary
  2. # write the modified text back to a file
  3.  
  4. import re
  5.  
  6. def replace_words(text, word_dic):
  7. """
  8. take a text and replace words that match a key in a dictionary with
  9. the associated value, return the changed text
  10. """
  11. rc = re.compile('|'.join(map(re.escape, word_dic)))
  12. def translate(match):
  13. return word_dic[match.group(0)]
  14. return rc.sub(translate, text)
  15.  
  16.  
  17. str1 = \
  18. """When we see a Space Shuttle sitting on its launch pad, there are two big
  19. booster rockets attached to the sides of the main fuel tank. These are solid
  20. rocket boosters, made by Thiokol at their factory in Utah. The engineers who
  21. designed the solid rocket boosters might have preferred to make them a bit
  22. fatter, but they had to be shipped by train from the factory to the launch
  23. site. The railroad line from the factory runs through a tunnel in the
  24. mountains. The boosters had to fit through that tunnel. The tunnel is
  25. slightly wider than the railroad track. The width of the railroad track
  26. came from the width of horse-drawn wagons in England, which were as wide
  27. as two horses' behinds. So, a major design feature of what is the world's
  28. most advanced transportation system was determined over two thousand years
  29. ago by the width of a horse's ass!
  30. """
  31.  
  32. test_file = "Mword1.txt"
  33. # create a test file for this example
  34. fout = open(test_file, "w")
  35. fout.write(str1)
  36. fout.close()
  37.  
  38. # read the file
  39. fin = open(test_file, "r")
  40. str2 = fin.read()
  41. fin.close()
  42.  
  43. # the dictionary has target_word:replacement_word pairs
  44. word_dic = {
  45. 'booster': 'rooster',
  46. 'rocket': 'pocket',
  47. 'solid': 'salted',
  48. 'tunnel': 'funnel',
  49. 'ship': 'slip'}
  50.  
  51. # call the function and get the changed text
  52. str3 = replace_words(str2, word_dic)
  53.  
  54. # test
  55. print str3
  56.  
  57. # write changed text back out
  58. fout = open("Mword2.txt", "w")
  59. fout.write(str3)
  60. fout.close()
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Feb 21st, 2007
0

Re: Replace words in a file

Thanks again, the last code example will work for me. I will have to read up on the module re to digest it all.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Jun 24th, 2009
0

Re: Replace words in a file

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??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Shraddha Kiran is offline Offline
5 posts
since Jun 2009
Aug 24th, 2010
0
Re: Replace words in a file
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cheezystix is offline Offline
1 posts
since Aug 2010
Oct 6th, 2010
0
Re: Replace words in a file
ignore
Last edited by romes87; Oct 6th, 2010 at 7:54 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
romes87 is offline Offline
18 posts
since Aug 2009
Oct 6th, 2010
0
Re: Replace words in a file
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"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
romes87 is offline Offline
18 posts
since Aug 2009
Oct 6th, 2010
0
Re: Replace words in a file
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)?
Python Syntax (Toggle Plain Text)
  1. input_text = """this is a fat cat
  2. that is a large pan
  3. that is a thick slice of bread"""
  4.  
  5. targets, new, output = ['fat','thick'], 'thin', ''
  6. for line in input_text.splitlines():
  7. for target in targets:
  8. if target in line:
  9. output +="//Modification\n"
  10. line = line.replace(target,new)
  11. output += line+'\n'
  12. print output
Featured Poster
Reputation Points: 687
Solved Threads: 748
Industrious Poster
pyTony is offline Offline
4,202 posts
since Apr 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Convert HEX into Binary Data
Next Thread in Python Forum Timeline: getopt python parser





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC