473,382 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

search and replace in a text file and save the changes

I am trying to search for a pattern in a text file and replace with another string.
I've opened the file in "r+" mode and used re.sub for replacement text.

The python output window is showing the desired result, but when I open the text file there is no change. The string that is to be replaced is not replaced in the text file.

Why is it so?
Expand|Select|Wrap|Line Numbers
  1. # !/usr/bin/python
  2.  
  3. import os,re
  4.  
  5. fName = r'LOCATION OF FILE'
  6. directory = os.path.dirname(fName)
  7.  
  8. os.chdir(directory)
  9.  
  10. fileName = os.path.basename(fName)
  11. print(fileName)
  12. o_File = open(fileName ,"r+")
  13.  
  14. with o_File as readFile:    
  15.     for searchpattern in readFile:
  16.  
  17.         found_Pattern = re.search("\s*\'[\d]+\==*[\d]*\.*[\d]*\==*[\d]*\.*[\d]*\'*",searchpattern,re.IGNORECASE)
  18.         if found_Pattern:
  19.             found_Pattern_Pat = found_Pattern.group()
  20.             print(value)
  21.             fValue = re.sub(r'==(.+)==','==100==',found_Pattern_Pat)
  22.             print(fValue)
  23.  
  24. o_File.close()
Mar 14 '14 #1
3 1402
bvdet
2,851 Expert Mod 2GB
Assuming your data file is not so large it cannot be stored in memory, compile a list of modified and unmodified lines of text (searchpattern would be one line of text). After closing o_File, open the file again or another suitable file in write mode and write the contents of the compiled list to it. Close the file object to flush the contents to disk.
Mar 14 '14 #2
@BVDET Thank you for the prompt reply.
Please can you tell me why I've to do so? In the tutorials, it is said that when you open a file in "r+" mode, the file is accessed in read as well as write mode.
But in my case, why it is not happening?
Please can you clarify me.

Thank You!
Mar 14 '14 #3
bvdet
2,851 Expert Mod 2GB
OK, "r+" instead of "r" is for reading and writing, but you still have to write the updated text out to the file. It may work on a line by line basis - I'm not sure. Let us know how it turns out.
Mar 14 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: alex | last post by:
Hi, I am looking for a way to populate an HTML table from an external local text file which looks like this: DATE/TIME LAT. LON. DEPTH. ML....
3
by: Josh | last post by:
I would like to search a text file on my server and then alert me on if the current date exist in that file. Any one have any code that searches text files. thanks in advance
4
by: suzy | last post by:
hello, i want to search a text file for an occurrence of a string and replace some text after the match, then save the file. eg: if the file is: <start> Hello my name is tony, I am 22....
1
by: Andrew | last post by:
Hi, im trying to create a small function which can create a binary tree from the entries in a text file and after that we can perform all the usual operations like search, insert and delete etc....
1
by: Prabhat | last post by:
Hello friends, I have one TEXT file that I use to log download information from website. The file will have unique Login ID for the downloads. When the user will come for next time for download...
13
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them...
3
by: mse07 | last post by:
hi all i search about statement in text file by this code : LineFlag = LineFlag + 1 Line Input #1, strLine If LineFlag = 15 Then If InStr(1, strLine, " Transfer completed...
8
by: ImanMan | last post by:
hi Am using the following code to search a textfile.The returned value will always be found in this case, is there any problem in the code?? Dim list As...
1
by: ahmed222too | last post by:
i use vb6 i want to make a backword search in text file i mean (by backword search) to begin the search from the last for example (i will go to school to learn more....) i want to find the last...
1
by: ogo796 | last post by:
Hi everyone i want to search the text file,using pregmatch() but it seems like it dose't serch.maybe my serching pattern is not right.look at the following code. Myfile look like this...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.