Python to search text file string and replace it 
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 -
#!/usr/bin/python
-
import string
-
s = open("/usr2/py/mount.txt","r+")
-
for line in s.readlines():
-
print line
-
string.replace(line, 'mickey','minnie')
-
print line
-
s.close()
-
However, the string.replace seems not working, any advice??? Thank you!
I am using CentOS 4.6 and the python is 2.3.4
| 
June 4th, 2009, 01:07 PM
|  | 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. - s = open("mount.txt").read()
-
s = s.replace('mickey', 'minnie')
-
f = open("mount.txt", 'w')
-
f.write(s)
-
f.close()
| 
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!!
| 
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 -
import fileinput
-
for line in fileinput.FileInput("file", inplace=1):
-
line=line.replace("old","new")
-
print line
-
| 
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: -
#!/usr/bin/python
-
import re
-
#fstab location, use w mode as need to overwrite whole file.
-
s = open("/usr2/py/mount.txt","r")
-
#temp txt file to store new mount.
-
tmpfile = open("/usr2/py/tmp.txt","a")
-
#new nas mount point
-
newmount = open("/usr2/py/newmount.txt","r")
-
#search pg-swnas1 line
-
for line in s.readlines():
-
if re.search("filer", line, re.IGNORECASE) != None:
-
print line
-
else:
-
tmpfile.write(line)
-
#read the latest mount point
-
readmount = newmount.read()
-
#append to temp file
-
tmpfile.write(readmount)
-
s.close()
-
tmpfile.close()
-
tmpfile = open("/usr2/py/tmp.txt","r")
-
readtmp = tmpfile.read()
-
s = open("/usr2/py/mount.txt","w")
-
s.write(readtmp)
-
-
tmpfile.close()
-
newmount.close()
-
-
Though the code works, but it seems not a clean code. Is there anyway to simplify it???
| 
June 25th, 2009, 10:51 AM
|  | 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): - mountList = open("/usr2/py/mount.txt", "r").readlines()
-
newmountList = open("/usr2/py/newmount.txt","r").readlines()
-
outputList = [item for item in mountList if "filer" not in item.lower()]
-
outputList.extend(newmountList)
-
f = open("/usr2/py/mount.txt","w")
-
f.write(''.join(outputList))
-
f.close()
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|