• Login
Community
  • Login

Help with find/replace python script

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
3 Posts 2 Posters 1.6k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G
    Gerrit Schut
    last edited by Apr 19, 2022, 3:45 PM

    Fellow Notepad++ Users,

    Could you please help me the the following search-and-replace problem I am having? I am not a programmer and I have very limited exposure to Python.

    I have a large number of parameters to replace in a text document all with the same place holder text of XXXXX.

    The data that I have looks something like:

    -------------------JOB1---------------------------
    parameter=( “data”,“PROD”,“XXXXX”,“2122”,-
    email_notify $MAIL “2122 XXXXX DATA_$ONE_UP job log” data_$ONE_UP.log
    -------------------JOB1END------------------------

    -------------------JOB2---------------------------
    parameter=( “data”,“PROD”,“XXXXX”,“2122”,-
    email_notify $MAIL “2122 XXXXX DATA_$ONE_UP job log” data_$ONE_UP.log
    -------------------JOB2END------------------------

    -------------------JOB3---------------------------
    parameter=( “data”,“PROD”,“XXXXX”,“2122”,-
    email_notify $MAIL “2122 XXXXX DATA_$ONE_UP job log” data_$ONE_UP.log
    -------------------JOB3END------------------------

    And what I would like it to look like:

    -------------------JOB1---------------------------
    parameter=( “data”,“PROD”,“PARM1”,“2122”,-
    email_notify $MAIL “2122 PARM1 DATA_$ONE_UP job log” data_$ONE_UP.log
    -------------------JOB1END------------------------

    -------------------JOB2---------------------------
    parameter=( “data”,“PROD”,“PARM2”,“2122”,-
    email_notify $MAIL “2122 PARM2 DATA_$ONE_UP job log” data_$ONE_UP.log
    -------------------JOB2END------------------------

    -------------------JOB3---------------------------
    parameter=( “data”,“PROD”,“PARM3”,“2122”,-
    email_notify $MAIL “2122 PARM3 DATA_$ONE_UP job log” data_$ONE_UP.log
    -------------------JOB3END------------------------

    I have used the following python script in the past to find and replace text in the past:

    with open('C:/Tmp/parm testing.txt') as f:
        for l in f:
            s = l.split()
            editor.replace(s[0], s[1])
    

    with the “parm testing.txt” list looking something like this:
    XXXXX PARM1
    XXXXX PARM2
    XXXXX PARM3

    Since the text that needs to be replaced is the same through the whole document the placeholder XXXXX is replaced only by the first PARM1 through the whole text and does not go down the list like I expected. This causes the output to look like:

    -------------------JOB1---------------------------
    parameter=( “data”,“PROD”,“PARM1”,“2122”,-
    email_notify $MAIL “2122 PARM1 DATA_$ONE_UP job log” data_$ONE_UP.log
    -------------------JOB1END------------------------

    -------------------JOB2---------------------------
    parameter=( “data”,“PROD”,“PARM1”,“2122”,-
    email_notify $MAIL “2122 PARM1 DATA_$ONE_UP job log” data_$ONE_UP.log
    -------------------JOB2END------------------------

    -------------------JOB3---------------------------
    parameter=( “data”,“PROD”,“PARM1”,“2122”,-
    email_notify $MAIL “2122 PARM1 DATA_$ONE_UP job log” data_$ONE_UP.log
    -------------------JOB3END------------------------

    What needs to be added to the script to make it go down the list in sequence so that JOB1 ends up with PARM1, JOB2 with PARM2, and JOB3 with PARM3? Or is there a better way to do this?

    I would really appreciate any help with this issue and I hope it will give me a better understanding of it.

    Thank you,
    Gerrit

    P 1 Reply Last reply Apr 19, 2022, 4:02 PM Reply Quote 1
    • P
      PeterJones @Gerrit Schut
      last edited by Apr 19, 2022, 4:02 PM

      @gerrit-schut ,

      Thanks for showing the before/expected/after (though it would have been nice to be in the black boxes as well)

      Looking at the PythonScript docs

      editor.replace(search, replace[, flags[, startPosition[, endPosition[, maxCount]]]])
      

      if you set the maxCount to 1, it will only do one replacement per call to editor.replace instead of replacing them all. So something like (untested):

      editor.replace(s[0],s[1], 0, 0, -1, 1)
      

      (using 0 for flags, 0 for start position is the beginning, and -1 means end-of-file for endPosition)

      G 1 Reply Last reply Apr 20, 2022, 1:20 PM Reply Quote 3
      • G
        Gerrit Schut @PeterJones
        last edited by Apr 20, 2022, 1:20 PM

        @peterjones

        This works perfect! I was able to test this with a number of different documents and was able to change the maxCount to fit the number of changes needed.

        You’ve literally reduced my work from weeks down to hours!

        THANK YOU SO MUCH!!!

        1 Reply Last reply Reply Quote 4
        1 out of 3
        • First post
          1/3
          Last post
        The Community of users of the Notepad++ text editor.
        Powered by NodeBB | Contributors