By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,180 Members | 1,051 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,180 IT Pros & Developers. It's quick & easy.

Find and replace in a file with regular expression

P: n/a
Hi everyone,
First I say that I serched and tryed everything but I cannot figure
out how I can do it.
I want to open a a file (not necessary a txt) and find and replace a
string.
I can do it with:

import fileinput, string, sys
fileQuery = "Text.txt"
sourceText = '''SOURCE'''
replaceText = '''REPLACE'''
def replace(fileName, sourceText, replaceText):

file = open(fileName, "r")
text = file.read() #Reads the file and assigns the value to a
variable
file.close() #Closes the file (read session)
file = open(fileName, "w")
file.write(text.replace(sourceText, replaceText))
file.close() #Closes the file (write session)
print "All went well, the modifications are done"

replacemachine(fileQuery, sourceText, replaceText)

Now all went ok but I'm wondering if it's possible to replace text if /
sourceText/ match a regex.
Help me please!
Thx in advance

Jan 30 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
The re module is used for regular expressions. Something like this
should work (untested):

import fileinput, string, sys, re

fileQuery = "Text.txt"
sourceText = '''SOURCE'''
replaceText = '''REPLACE'''

def replace(fileName, sourceText, replaceText):
file = open(fileName, "r")
text = file.read() #Reads the file and assigns the value to a
variable
file.close() #Closes the file (read session)

file = open(fileName, "w")
file.write(re.sub(sourceText, replaceText,text))
file.close() #Closes the file (write session)

print "All went well, the modifications are done"

replacemachine(fileQuery, sourceText, replaceText)

Jan 30 '07 #2

P: n/a
Just in case you didn't think about it there is a plain replace method for strings

How to quick-search this method with 'dir'
>>dir("")
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'replace', 'rfind', 'rindex',
'rjust', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>help("".replace)
Help on built-in function replace:

replace(...)
S.replace (old, new[, maxsplit]) -string

Return a copy of string S with all occurrences of substring
old replaced by new. If the optional argument maxsplit is
given, only the first maxsplit occurrences are replaced.

and now how to apply it:
new_text = open(fileName).read().replace("SOURCE", "REPLACE")

which is the preferred method for a simple task like this.

TOXiC wrote:
Hi everyone,
First I say that I serched and tryed everything but I cannot figure
out how I can do it.
I want to open a a file (not necessary a txt) and find and replace a
string.
I can do it with:

import fileinput, string, sys
fileQuery = "Text.txt"
sourceText = '''SOURCE'''
replaceText = '''REPLACE'''
def replace(fileName, sourceText, replaceText):
Now how to solve it with a simple regular expression:
>>import re
re_replace = re.compile("SOURCE").sub

txt = " SOURCE SOURCE \n SOURCE "

print re_replace("REPLACE", txt)
' REPLACE REPLACE \n REPLACE '
>>new_text = re_replace("REPLACE", open(fileName).read())
A regular expression for this task is kind of overkill. Mastering
regular expression is the efford very worth.

Wolfgang Grafen
Jan 31 '07 #3

P: n/a
TOXiC wrote:
Hi everyone,
First I say that I serched and tryed everything but I cannot figure
out how I can do it.
I want to open a a file (not necessary a txt) and find and replace a
string.
I can do it with:

import fileinput, string, sys
fileQuery = "Text.txt"
sourceText = '''SOURCE'''
replaceText = '''REPLACE'''
def replace(fileName, sourceText, replaceText):

file = open(fileName, "r")
text = file.read() #Reads the file and assigns the value to a
variable
file.close() #Closes the file (read session)
file = open(fileName, "w")
file.write(text.replace(sourceText, replaceText))
file.close() #Closes the file (write session)
print "All went well, the modifications are done"

replacemachine(fileQuery, sourceText, replaceText)

Now all went ok but I'm wondering if it's possible to replace text if /
sourceText/ match a regex.
Help me please!
Thx in advance

Try this:
>>import SE # from http://cheeseshop.python.org/pypi/SE/2.3
>>replacements = 'SOURCE=REPLACE "another source=another replace"
~[0-9]+~=>>int<< ~[0-9]+\\.[0-9]+~=>>float<< ~'
# Define as many replacements as you like. Identify regexes placing
them between '~'
>>Stream_Editor = SE.SE (replacements)
>>Stream_Editor (input_file, output_file)
That's it.
PS 1: Your Stream_Editor accepts strings as well as file names and then
returns a string by default. This is a great help for developing
substitution sets interactively.
>>print Stream_Editor ('''
If it works, this SOURCE should read REPLACE
and another source should become another replace
and this 123456789 should become >>int<<
and this 12345.6789 is a float and so should read >>float<<.''')

If it works, this REPLACE should read REPLACE
and another replace should become another replace
and this >>int<< should become >>int<<
and this >>float<< is a float and so should read >>float<<.

PS 2: It is convenient to keep large and frequently used substitution
sets in text files. The SE constructor accepts a file name instead of
the replacements string:
>>Stream_Edtor = SE.SE ('path/replacement_definitions_file')

Regards

Frederic

Feb 3 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.