Community
    • Login

    Help with find/replace python script

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 2 Posters 1.5k 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.
    • Gerrit SchutG
      Gerrit Schut
      last edited by

      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

      PeterJonesP 1 Reply Last reply Reply Quote 1
      • PeterJonesP
        PeterJones @Gerrit Schut
        last edited by

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

        Gerrit SchutG 1 Reply Last reply Reply Quote 3
        • Gerrit SchutG
          Gerrit Schut @PeterJones
          last edited by

          @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
          • First post
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors