search and replace using python

Kam

I have the following file "template.txt"

function FA()
{
    if(){...}
    message=[msg]
    message=[msg]
}
function FB()
{
    if(){...}
    message=[msg]
    message=[msg]
}
function FC()
{
    if(){...}
    message=[msg]
    message=[msg]
}

I would like to do this:

./script.py --function FB --message TEST

and get this result:

function FA()
{
    if(){...}
    message=[msg]
    message=[msg]
}
function FB()
{
    if(){...}
    message=TEST
    message=TEST
}
function FC()
{
    if(){...}
    message=[msg]
    message=[msg]
}

I can now using getopt retrieve all the options and arguments properly but I can't figure out how to achieve the above behavior elegantly. Any ideas? Are there any libraries that can help me with this?

I was able to achieve this behavior using AWK but now I need it in python. In AWK you can go to a specific line (e.g. function FC()) and start replacing from there until you hit another function. I can't seem to figure this out in python.

I am also wondering if there's a better approach to this problem.

James Robinson

Once you get your variables, and have them sanitized properly you can do something like this.

def templater(template,function,message):
    template = template.split('function')
    for i,f in enumerate(template):
        if function in f:
            template[i] = f.replace('[msg]',message)
    return 'function'.join(template)

Edit: As far as a better approach, you should consider creating your template using the formatting mini language http://docs.python.org/2/library/string.html#formatspec or an actual templating language such as jinja2 http://jinja.pocoo.org/docs/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Search and replace using regular expressions in Python

From Dev

Search and replace using awk

From Dev

Python search and replace in a string

From Dev

Search and replace a string pattern in python using regular expressions

From Dev

find and replace "tabs" using search and replace in nano

From Dev

Using regex in the "replace" part of a search/replace?

From Dev

Backward search and replace using sed

From Dev

wildcard search using replace function

From Dev

search and replace using grep (not sed)

From Dev

Search and replace using prename or sed

From Dev

Python Search text file & replace

From Dev

Python search and replace in configuration file

From Dev

Python Regex - search and replace matches

From Dev

Search and replace a word in a string in python

From Dev

Regex search and replace substring in Python

From Dev

Search and replace previous line python

From Dev

Using fileinput (Python) for a search-and-replace while also sending messages to console

From Dev

Search the word, and replace the whole line containing the word in a file in Python using fileinput

From Dev

Questions about using Regex Search & Replace in gedit

From Dev

Search and replace with 2 separate files using AWK

From Dev

Using Sublime's regex search/replace with \Z

From Dev

Search and replace in multiple files using vim

From Dev

Replace using VIM, reuse part of the search pattern

From Dev

Questions about using Regex Search & Replace in gedit

From Dev

Search and replace within a file using PCRE in C

From Dev

Excel: Using a formula to search a column and replace cells

From Dev

Search and replace with 2 separate files using AWK

From Dev

search and replace string using batch or perl script

From Dev

Bash while loop search and replace using sed

Related Related

  1. 1

    Search and replace using regular expressions in Python

  2. 2

    Search and replace using awk

  3. 3

    Python search and replace in a string

  4. 4

    Search and replace a string pattern in python using regular expressions

  5. 5

    find and replace "tabs" using search and replace in nano

  6. 6

    Using regex in the "replace" part of a search/replace?

  7. 7

    Backward search and replace using sed

  8. 8

    wildcard search using replace function

  9. 9

    search and replace using grep (not sed)

  10. 10

    Search and replace using prename or sed

  11. 11

    Python Search text file & replace

  12. 12

    Python search and replace in configuration file

  13. 13

    Python Regex - search and replace matches

  14. 14

    Search and replace a word in a string in python

  15. 15

    Regex search and replace substring in Python

  16. 16

    Search and replace previous line python

  17. 17

    Using fileinput (Python) for a search-and-replace while also sending messages to console

  18. 18

    Search the word, and replace the whole line containing the word in a file in Python using fileinput

  19. 19

    Questions about using Regex Search & Replace in gedit

  20. 20

    Search and replace with 2 separate files using AWK

  21. 21

    Using Sublime's regex search/replace with \Z

  22. 22

    Search and replace in multiple files using vim

  23. 23

    Replace using VIM, reuse part of the search pattern

  24. 24

    Questions about using Regex Search & Replace in gedit

  25. 25

    Search and replace within a file using PCRE in C

  26. 26

    Excel: Using a formula to search a column and replace cells

  27. 27

    Search and replace with 2 separate files using AWK

  28. 28

    search and replace string using batch or perl script

  29. 29

    Bash while loop search and replace using sed

HotTag

Archive