Connecting Tech Pros Worldwide Help | Site Map

Python to search text file string and replace it

  #1  
Old June 4th, 2009, 12:21 PM
Newbie
 
Join Date: Aug 2007
Posts: 19
Hi ,

i am in the middle of creating the script to match the string and replace it

in mount.txt
mickey:/work1 /work1 bla bla bla
mickey:/work2 /work2 bla bla bla
micket:/job /job bla bla bla


Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2. import string
  3. s = open("/usr2/py/mount.txt","r+")
  4. for line in s.readlines():
  5.    print line
  6.    string.replace(line, 'mickey','minnie')
  7.    print line
  8. s.close()
  9.  
However, the string.replace seems not working, any advice??? Thank you!

I am using CentOS 4.6 and the python is 2.3.4
  #2  
Old June 4th, 2009, 01:07 PM
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,545

re: Python to search text file string and replace it


Hello DannyMc,

string.replace() does not modify the argument string in place, but returns a modified string. You must make an assignment. Unless your file is very large, I would suggest updating the text in the file by reading the file, modifying the text, and writing the modified text back to the file. You can use string method replace() instead of importing the string module.
Expand|Select|Wrap|Line Numbers
  1. s = open("mount.txt").read()
  2. s = s.replace('mickey', 'minnie')
  3. f = open("mount.txt", 'w')
  4. f.write(s)
  5. f.close()
  #3  
Old June 5th, 2009, 10:25 AM
Newbie
 
Join Date: Aug 2007
Posts: 19

re: Python to search text file string and replace it


thanks for the solution bvdet!!
  #4  
Old June 11th, 2009, 01:06 PM
Expert
 
Join Date: Apr 2006
Posts: 512

re: Python to search text file string and replace it


you can use fileinput for inplace editing
Expand|Select|Wrap|Line Numbers
  1. import fileinput
  2. for line in fileinput.FileInput("file", inplace=1):
  3.     line=line.replace("old","new")
  4.     print line
  5.  
  #5  
Old June 25th, 2009, 07:57 AM
Newbie
 
Join Date: Aug 2007
Posts: 19

re: Python to search text file string and replace it


Hi guys, i managed to solve my problem with the following scripts:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2. import re
  3. #fstab location, use w mode as need to overwrite whole file.
  4. s = open("/usr2/py/mount.txt","r")
  5. #temp txt file to store new mount.
  6. tmpfile = open("/usr2/py/tmp.txt","a")
  7. #new nas mount point
  8. newmount = open("/usr2/py/newmount.txt","r")
  9. #search pg-swnas1 line
  10. for line in s.readlines():
  11.     if re.search("filer", line, re.IGNORECASE) != None:
  12.         print line
  13.     else:
  14.         tmpfile.write(line)
  15. #read the latest mount point
  16. readmount = newmount.read()
  17. #append to temp file
  18. tmpfile.write(readmount)
  19. s.close()
  20. tmpfile.close()
  21. tmpfile = open("/usr2/py/tmp.txt","r")
  22. readtmp = tmpfile.read()
  23. s = open("/usr2/py/mount.txt","w")
  24. s.write(readtmp)
  25.  
  26. tmpfile.close()
  27. newmount.close()
  28.  
  29.  
Though the code works, but it seems not a clean code. Is there anyway to simplify it???
  #6  
Old June 25th, 2009, 10:51 AM
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,545

re: Python to search text file string and replace it


I am a bit unclear about what mount.txt and newmount.txt contain and what you need to replace. You do not need re. You can test for membership with the in operator. If I understand correctly, this should work (untested):
Expand|Select|Wrap|Line Numbers
  1. mountList = open("/usr2/py/mount.txt", "r").readlines()
  2. newmountList = open("/usr2/py/newmount.txt","r").readlines()
  3. outputList = [item for item in mountList if "filer" not in item.lower()]
  4. outputList.extend(newmountList)
  5. f = open("/usr2/py/mount.txt","w")
  6. f.write(''.join(outputList))
  7. f.close()
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seeking assistance - string processing. billpaterson2006@googlemail.com answers 11 November 14th, 2006 05:35 PM
RegEx conditional search and replace Martin Evans answers 6 July 6th, 2006 09:45 PM
[perl-python] find & replace strings for all files in a dir Xah Lee answers 1 July 18th, 2005 09:55 PM
Request for Feedback; a module making it easier to use regular expressions. Kenneth McDonald answers 1 July 18th, 2005 09:55 PM