• Login
Community
  • Login

another replacement request for help

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
40 Posts 4 Posters 3.9k 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.
  • E
    Ekopalypse @Carlos J. Segnini R.
    last edited by Aug 21, 2020, 8:07 PM

    @Carlos-J-Segnini-R

    I would think this is a find/replace action.
    Find: <UWI>Aery #B1H</UWI>\R\h*<WellType>\K.+?(?=</WellType>)
    Replace:Gas
    tick regular expression

    Because of the usage of the \K syntax you can only use replace all button.

    C 1 Reply Last reply Aug 21, 2020, 8:28 PM Reply Quote 2
    • C
      Carlos J. Segnini R. @Ekopalypse
      last edited by Aug 21, 2020, 8:28 PM

      @Ekopalypse said in another replacement request for help:

      \R\h

      Thanks It works, but I would need to do one by one.
      Again I would have a list of different names (Aery #B1H, Aery #D2H, etc) so I was looking for another magic script haha

      E 1 Reply Last reply Aug 21, 2020, 8:30 PM Reply Quote 0
      • E
        Ekopalypse @Carlos J. Segnini R.
        last edited by Aug 21, 2020, 8:30 PM

        @Carlos-J-Segnini-R

        but then we would need some kind of mapping again,
        otherwise the script does not know when to use what. :-)

        1 Reply Last reply Reply Quote 0
        • C
          Carlos J. Segnini R.
          last edited by Aug 21, 2020, 8:37 PM

          So this is how the xml file looks
          e6afaf3a-b1ef-4205-bb9c-515f61d7122f-image.png
          <Well replace=“false”>
          <UWI>Alexander Gas Unit 1 #2</UWI>
          <WellType>Oil</WellType>
          <ReserveStatusCollection>
          <Working>
          <PrimaryProduct>Oil</PrimaryProduct>
          <WellResCatCollection>

          and this is how the replacement file would be
          7c50836c-5aa4-47e1-ac7e-26568264fb30-image.png
          7dda6e13-179d-4e43-b772-a88ae007c825-image.png
          Alexander Gas Unit 1 #2 Oil Gas
          Alexander - Wessendorff 1 A3H Oil Gas
          Alexander - Wessendorff 1 A4H Oil Gas

          So the script would look for the first column (Well name), and then replace the word in the second column for the one in the third column:

          <WellType>Oil</WellType> to <WellType>Gas</WellType>

          Thanks again, you’re saving my life

          E 1 Reply Last reply Aug 21, 2020, 9:03 PM Reply Quote 0
          • E
            Ekopalypse @Carlos J. Segnini R.
            last edited by Aug 21, 2020, 9:03 PM

            @Carlos-J-Segnini-R

            Is the oil column really needed?
            I mean, does it needs to be checked or can the script always look for company and then replace whatever is in WellType with the value from the Gas column?

            1 Reply Last reply Reply Quote 1
            • C
              Carlos J. Segnini R.
              last edited by Carlos J. Segnini R. Aug 21, 2020, 9:06 PM Aug 21, 2020, 9:05 PM

              Edit:
              Thinking about, no, no need for that column

              E 4 Replies Last reply Aug 21, 2020, 9:06 PM Reply Quote 0
              • E
                Ekopalypse @Carlos J. Segnini R.
                last edited by Aug 21, 2020, 9:06 PM

                This post is deleted!
                1 Reply Last reply Reply Quote 0
                • E
                  Ekopalypse @Carlos J. Segnini R.
                  last edited by Aug 21, 2020, 9:08 PM

                  @Carlos-J-Segnini-R

                  so you can get rid of the second column and the only thing we need to change is the find expression, I guess.

                  1 Reply Last reply Reply Quote 1
                  • E
                    Ekopalypse @Carlos J. Segnini R.
                    last edited by Aug 21, 2020, 9:12 PM

                    @Carlos-J-Segnini-R

                    from Npp import editor1, editor2
                    
                    replacements = dict(line.split('\t') for line in editor2.getText().splitlines() if line)
                    
                    def replace_with(m):
                        return replacements[m.group(1)]
                    
                    # search_for = '(?<=<UWI>).+?(?=</UWI>)'
                    search_for = '<UWI>(.+)</UWI>\R\h*<WellType>\K.+?(?=</WellType>)'
                    editor1.rereplace(search_for, replace_with)
                    
                    1 Reply Last reply Reply Quote 2
                    • E
                      Ekopalypse @Carlos J. Segnini R.
                      last edited by Aug 21, 2020, 9:17 PM

                      @Carlos-J-Segnini-R

                      btw. if you are interested how these regex search work see here for a pretty good description.

                      1 Reply Last reply Reply Quote 2
                      • C
                        Carlos J. Segnini R.
                        last edited by Aug 21, 2020, 9:41 PM

                        Thanks again for your help.
                        For some reason this time it isn’t working.
                        I will try and fix it.

                        a4f54196-4f8f-444f-a7b7-15048ba35f66-image.png

                        E 1 Reply Last reply Aug 21, 2020, 9:47 PM Reply Quote 0
                        • E
                          Ekopalypse @Carlos J. Segnini R.
                          last edited by Aug 21, 2020, 9:47 PM

                          @Carlos-J-Segnini-R

                          Can you open the PythonScript console (plugin->PythonScript->Show Console) to see if there is an error?
                          The replacement file has company and gas/oil tab separated, correct?

                          C 1 Reply Last reply Aug 21, 2020, 9:50 PM Reply Quote 0
                          • C
                            Carlos J. Segnini R. @Ekopalypse
                            last edited by Carlos J. Segnini R. Aug 21, 2020, 9:51 PM Aug 21, 2020, 9:50 PM

                            @Ekopalypse here is the log.
                            I think it is because not all the wells need to be changed, so the first one in the file (Aery #B1H) does not appear in the second file, so the script is stopping. I will try leaving all wells, even those that doesnt need to be changed

                            Traceback (most recent call last):
                            File “C:\Users\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\find_replace.py”, line 10, in <module>
                            editor1.rereplace(search_for, replace_with)
                            File “C:\Users\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\find_replace.py”, line 6, in replace_with
                            return replacements[m.group(1)]
                            KeyError: ‘Aery #B1H’

                            And yes, the other file is separated by tabs.

                            E 3 Replies Last reply Aug 21, 2020, 9:52 PM Reply Quote 0
                            • E
                              Ekopalypse @Carlos J. Segnini R.
                              last edited by Aug 21, 2020, 9:52 PM

                              @Carlos-J-Segnini-R

                              Yes, that makes sense then we need to take another approach where we
                              create the searches based on the second list. Gimme a minute

                              1 Reply Last reply Reply Quote 0
                              • E
                                Ekopalypse @Carlos J. Segnini R.
                                last edited by Aug 21, 2020, 9:54 PM

                                @Carlos-J-Segnini-R

                                from Npp import editor1, editor2
                                
                                replacements = dict(line.split('\t') for line in editor2.getText().splitlines() if line)
                                
                                def replace_with(m):
                                    return replacements[m.group(1)]
                                
                                # search_for = '(?<=<UWI>).+?(?=</UWI>)'
                                for company in replacements.keys():
                                    search_for = '<UWI>({})</UWI>\R\h*<WellType>\K.+?(?=</WellType>)'.format(company)
                                    editor1.rereplace(search_for, replace_with)
                                
                                1 Reply Last reply Reply Quote 2
                                • E
                                  Ekopalypse @Carlos J. Segnini R.
                                  last edited by Aug 21, 2020, 10:11 PM

                                  @Carlos-J-Segnini-R

                                  depending on the file size a faster solution might be this

                                  from Npp import editor1, editor2
                                  
                                  replacements = dict(line.split('\t') for line in editor2.getText().splitlines() if line)
                                  
                                  def replace_with(m):
                                      company = replacements.get(m.group(1), None)
                                      if company:
                                          return replacements[m.group(1)]
                                      else:
                                          return m.group()
                                  
                                  # search_for = '(?<=<UWI>).+?(?=</UWI>)'
                                  search_for = '<UWI>(.+)</UWI>\R\h*<WellType>\K.+?(?=</WellType>)'
                                  editor1.rereplace(search_for, replace_with)
                                  

                                  Only scanning the text one time and in case the company found is
                                  not in the replacements dictionary we replace it with what was found.
                                  Btw. its midnight here - good night.

                                  1 Reply Last reply Reply Quote 2
                                  • C
                                    Carlos J. Segnini R.
                                    last edited by Aug 21, 2020, 10:14 PM

                                    The first one worked!
                                    Thanks so much for your help, I owe you a beer!
                                    Have a good night

                                    1 Reply Last reply Reply Quote 0
                                    • C
                                      Carlos J. Segnini R.
                                      last edited by Aug 21, 2020, 10:30 PM

                                      By the way, I went back and edited the script to run a second time looking for another instance below.

                                      I’m sure there are better ways to jump three lines, but it’s late and I needed to finish. It worked! haha

                                      from Npp import editor1, editor2
                                      
                                      replacements = dict(line.split('\t') for line in editor2.getText().splitlines() if line)
                                      
                                      def replace_with(m):
                                          return replacements[m.group(1)]
                                      
                                      # search_for = '(?<=<UWI>).+?(?=</UWI>)'
                                      for company in replacements.keys():
                                          search_for = '<UWI>({})</UWI>\R\h*.*\R\h*.*\R\h*.*\R\h*<PrimaryProduct>\K.+?(?=</PrimaryProduct>)'.format(company)
                                          editor1.rereplace(search_for, replace_with)
                                      
                                      E 1 Reply Last reply Aug 24, 2020, 10:39 AM Reply Quote 1
                                      • E
                                        Ekopalypse @Carlos J. Segnini R.
                                        last edited by Aug 24, 2020, 10:39 AM

                                        @Carlos-J-Segnini-R

                                        I’m sure there are better ways to jump three lines,

                                        In the end, what really counts is whether it does what it is supposed to do, right?
                                        One, of several alternatives would be for example

                                        <UWI>(.*)</UWI>(?s)(\R.+?)<PrimaryProduct>\K.+?(?=</PrimaryProduct>)

                                        but whether this is better or worse depends on the real data.

                                        1 Reply Last reply Reply Quote 3
                                        • E
                                          Ekopalypse
                                          last edited by Aug 24, 2020, 11:00 AM

                                          @Ekopalypse said in another replacement request for help:

                                          <UWI>(.*)</UWI>(?s)(\R.+?)<PrimaryProduct>\K.+?(?=</PrimaryProduct>)

                                          to be used in the script it would be

                                          '<UWI>({})</UWI>(?s)(\R.+?)<PrimaryProduct>\K.+?(?=</PrimaryProduct>)'
                                          
                                          1 Reply Last reply Reply Quote 3
                                          20 out of 40
                                          • First post
                                            20/40
                                            Last post
                                          The Community of users of the Notepad++ text editor.
                                          Powered by NodeBB | Contributors