Find and replace in a file with regular expression

Discussion in 'Python' started by TOXiC, Jan 30, 2007.

  1. TOXiC

    TOXiC Guest

    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
    TOXiC, Jan 30, 2007
    #1
    1. Advertising

  2. TOXiC

    Matimus Guest

    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)
    Matimus, Jan 30, 2007
    #2
    1. Advertising

  3. 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
    Wolfgang Grafen, Jan 31, 2007
    #3
  4. 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
    Frederic Rentsch, Feb 3, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,272
  2. Mark
    Replies:
    0
    Views:
    298
  3. Replies:
    6
    Views:
    845
    John C. Bollinger
    Oct 7, 2005
  4. Jimmy
    Replies:
    25
    Views:
    756
    Jeff Higgins
    May 26, 2010
  5. Subra

    Find replace : Regular expression

    Subra, Jun 12, 2007, in forum: Perl Misc
    Replies:
    5
    Views:
    147
    Gunnar Hjalmarsson
    Jun 13, 2007
Loading...

Share This Page