Find and replace in a file with regular expression

T

TOXiC

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
 
M

Matimus

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)
 
W

Wolfgang Grafen

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'['__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 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.
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:

A regular expression for this task is kind of overkill. Mastering
regular expression is the efford very worth.

Wolfgang Grafen
 
F

Frederic Rentsch

TOXiC said:
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:
~[0-9]+~=>>int<< ~[0-9]+\\.[0-9]+~=>>float<< ~'
# Define as many replacements as you like. Identify regexes placing
them between '~'

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.
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:


Regards

Frederic
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top