Find and replace in a file with regular expression

Frederic Rentsch anthra.norell at vtxmail.ch
Sat Feb 3 07:37:45 EST 2007


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








More information about the Python-list mailing list