• Login
Community
  • Login

Search and Replace

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
search miltiplereplacedelimiterselect
27 Posts 4 Posters 6.7k 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.
  • B
    Bert Barber
    last edited by Bert Barber Apr 15, 2020, 7:57 PM Apr 15, 2020, 7:56 PM

    I have created a space delimited file with two columns (*.prn)

    fe5b1c14-5743-4163-ac73-9e793b72fd90-image.png

    For every instance (find) of the text in column 1, I want to replace it with the text in column 2. The file I am searching has over 9,000 lines.

    I have the following in a Python script I am running. Really strange, NONE of the words are being replaced but some of the ids (ending with “__” ) are being replaced.

    with open(‘D:/Note Pad Plus Macros/Conversion Data.prn’) as f:
    for l in f:
    s = l.split()
    editor.replace(r’\b’ + s[0] + r’\b’, s[1])

    Any suggestions would be greatly appreciated. I have over 1,000 more changes to make and cutting and pasting is getting very tiring.

    Thanks so very much in advance for any assistance…

    A 1 Reply Last reply Apr 15, 2020, 8:01 PM Reply Quote 0
    • A
      Alan Kilborn @Bert Barber
      last edited by Alan Kilborn Apr 15, 2020, 8:02 PM Apr 15, 2020, 8:01 PM

      @Bert-Barber

      One problem I see immediately is that your data has spaces inside each column, so your columns can’t be “space delimited”. Well, they can be, but nothing will be able to tell a space between the columns from a space within a column.

      For the first example of this, see the Aged Out in column 1.

      You have to solve this problem before you can make any progress on this.

      Also, not sure how you are showing us the data, Excel? If so, how does that relate to what the same data looks like (and actually IS) in Notepad++?

      B 1 Reply Last reply Apr 15, 2020, 8:06 PM Reply Quote 1
      • B
        Bert Barber @Alan Kilborn
        last edited by Apr 15, 2020, 8:06 PM

        @Alan-Kilborn Thank you for your quick response. Can the code:

        with open(‘D:/Note Pad Plus Macros/Conversion Data.prn’) as f:
        for l in f:
        s = l.split()
        editor.replace(r’\b’ + s[0] + r’\b’, s[1])

        be changed to work with a comma delimited file?

        The data shown is a screen capture of excel file.

        A 2 Replies Last reply Apr 15, 2020, 8:12 PM Reply Quote 0
        • P
          PeterJones
          last edited by Apr 15, 2020, 8:12 PM

          Python 2.7 split documentation will help you figure out what argument to pass to split to make it split on commas instead of whitespace.

          Please note that split-based parsing of CSV is a poor substitute for a full CSV library , because while splitting on commas can handle the simple forms of CSV, documents that have commas or newlines embedded in cells will confuse all but a fully-vetted library.

          B 1 Reply Last reply Apr 15, 2020, 8:22 PM Reply Quote 1
          • A
            Alan Kilborn @Bert Barber
            last edited by Alan Kilborn Apr 15, 2020, 8:12 PM Apr 15, 2020, 8:12 PM

            @Bert-Barber

            Can the code:
            be changed to work with a comma delimited file?

            If there is going to be no commas in your data except the one which separates the columns, then yes, try:

            s = l.split(',')

            Also, I’d remove the r'\b' stuff. You don’t need it, and it won’t work with editor.replace anyway (you’d need editor.rereplace). But again, you don’t need it.

            A 1 Reply Last reply Apr 15, 2020, 8:16 PM Reply Quote 1
            • A
              Alan Kilborn @Alan Kilborn
              last edited by Alan Kilborn Apr 15, 2020, 8:17 PM Apr 15, 2020, 8:16 PM

              Maybe in your generated lookup data file do a file search for (?-s),.*?, and see if you get any hits. If you don’t you’ll know your data only has one comma per line (the one from the export process) and you are good to go with a simple “split” based scheme. Otherwise, yeah, Peter is right in that it gets way more complicated. But I’d think by now you are fairly close to the final solution. Nice to see someone other than the regulars here do something with Python!

              B A 2 Replies Last reply Apr 15, 2020, 8:21 PM Reply Quote 1
              • A
                Alan Kilborn @Bert Barber
                last edited by Apr 15, 2020, 8:20 PM

                @Bert-Barber

                If you hadn’t gone with a Python solution, a good reference for a table-driven replacement is HERE.

                1 Reply Last reply Reply Quote 0
                • B
                  Bert Barber @Alan Kilborn
                  last edited by Apr 15, 2020, 8:21 PM

                  @Alan-Kilborn Thanks!

                  1 Reply Last reply Reply Quote 1
                  • B
                    Bert Barber @PeterJones
                    last edited by Apr 15, 2020, 8:22 PM

                    @PeterJones Thanks for your reply!

                    1 Reply Last reply Reply Quote 1
                    • A
                      Alan Kilborn @Alan Kilborn
                      last edited by Apr 15, 2020, 8:23 PM

                      @Alan-Kilborn said in Search and Replace:

                      in your generated lookup data file do a file search for (?-s),.*?, and see if you get any hits

                      Hopefully it is obvious but that would be a search with Search mode set to Regular Expression

                      1 Reply Last reply Reply Quote 1
                      • E
                        Ekopalypse
                        last edited by Ekopalypse Apr 15, 2020, 8:36 PM Apr 15, 2020, 8:35 PM

                        @Bert-Barber

                        maybe I’m missing something but you CANNOT use editor.replace on a file.

                        A 1 Reply Last reply Apr 15, 2020, 8:42 PM Reply Quote 1
                        • A
                          Alan Kilborn @Ekopalypse
                          last edited by Apr 15, 2020, 8:42 PM

                          @Ekopalypse said in Search and Replace:

                          maybe I’m missing something

                          I took it as the OP has the file they are applying the lookup-replacement on active in Notepad++ when they run the script. We’ve lost the indentation for the OP’s script because of they posting method used, but if it is indented the way I would think, I believe the (rough) logic works?

                          B 1 Reply Last reply Apr 16, 2020, 10:29 AM Reply Quote 1
                          • E
                            Ekopalypse
                            last edited by Apr 15, 2020, 8:50 PM

                            Hmm … strange way, … and potentially dangerous.
                            You open the file and do the replace in the scintilla buffer.
                            As long as it is not saved while still open …

                            A 1 Reply Last reply Apr 15, 2020, 9:50 PM Reply Quote 0
                            • A
                              Alan Kilborn @Ekopalypse
                              last edited by Alan Kilborn Apr 15, 2020, 9:51 PM Apr 15, 2020, 9:50 PM

                              @Ekopalypse said in Search and Replace:

                              You open the file and do the replace in the scintilla buffer.

                              My understanding of OP’s scenario is:

                              • file to be modified is the active Notepad++ tab
                              • the script uses non-Notepad++ Python code to open and read the file that contains the lookup table (this file is NOT currently open in Notepad++)
                              • script used Notepad++ object to modify the Scintilla buffer of the active file

                              I don’t see any issue whatsoever with that process. Of course it is always good to have a backup copy of data until you verify that things are correct.

                              Of course one could argue “why not do it all in straight Python and avoid Notepad++ entirely”. And that’s a perfectly valid route as well.

                              B E 2 Replies Last reply Apr 16, 2020, 10:31 AM Reply Quote 1
                              • B
                                Bert Barber @Alan Kilborn
                                last edited by Apr 16, 2020, 10:29 AM

                                @Alan-Kilborn Yes the file I am applying the lookup to is active in Notepad ++.

                                I do sincerely appreciate the help!

                                1 Reply Last reply Reply Quote 0
                                • B
                                  Bert Barber @Alan Kilborn
                                  last edited by Apr 16, 2020, 10:31 AM

                                  @Alan-Kilborn You understanding or the process I am attempting is exactly correct. I am experimenting now and will post response shortly.

                                  Once again I do sincerely appreciate the suggestions and recommendations, I am learning!

                                  E 1 Reply Last reply Apr 16, 2020, 10:42 AM Reply Quote 0
                                  • E
                                    Ekopalypse @Alan Kilborn
                                    last edited by Apr 16, 2020, 10:35 AM

                                    @Alan-Kilborn said in Search and Replace:

                                    I don’t see any issue whatsoever with that process.

                                    If I’m allowed to use an analogy.
                                    Of course I can use a gas grill and put coal in it to make my barbecue,
                                    but should I?
                                    The result could be unforeseen.
                                    What if the OP is under the impression that this is the correct way of doing this kind of stuff?

                                    1 Reply Last reply Reply Quote 0
                                    • E
                                      Ekopalypse @Bert Barber
                                      last edited by Apr 16, 2020, 10:42 AM

                                      @Bert-Barber said in Search and Replace:

                                      I am learning

                                      If your intention is to use the PythonScript plugin and a python script
                                      to manipulate a file then the “correct” way of doing this is either
                                      to use

                                      with open('FILENAME','MODE') as f:
                                          x = f.read() or f.write() etc.
                                      

                                      if you are not loading it into npp,
                                      but if you have it open in npp and want to see the results
                                      you normally do something like

                                      lines = editor.getText().splitlines()
                                      for line in lines:
                                          print(line.upper())
                                      
                                      1 Reply Last reply Reply Quote 1
                                      • A
                                        Alan Kilborn
                                        last edited by Apr 16, 2020, 12:07 PM

                                        @Ekopalypse

                                        Analogy noted, I think I did that once, with an old gas grill I was about ready to discard. Or I thought about it.

                                        Back to code, I still see absolutely nothing wrong.
                                        In fact, it may be the cleanest and shortest way for the OP to achieve his goal (with code).
                                        Is it the way I would have done it? Probably not.

                                        The bottom line is we must agree to disagree, or one of us is still missing something.
                                        Perhaps @PeterJones could offer an opinion to perhaps “turn on the lights” on this.

                                        P 1 Reply Last reply Apr 16, 2020, 1:16 PM Reply Quote 2
                                        • P
                                          PeterJones @Alan Kilborn
                                          last edited by Apr 16, 2020, 1:16 PM

                                          @Alan-Kilborn & @Ekopalypse ,

                                          I doubt I’ll “turn the lights on”.

                                          The OP claims to be using the procedure Alan outlined (reading an external file to manipulate how the internal Notepad++/Scintilla file is being edited). As such, I don’t think the code isn’t necessarily bad (ie, OP hasn’t started dumping propane in the charcoal grill), but it’s not the best (why put charcoal in your broken gas grill – even if the propane isn’t connected – when you have a made-for-the-task charcoal grill on the porch already? If your excuse is that no one told you the black R2D2-looking thing on the porch was actually a charcoal grill, well, now you know.).

                                          Personally, I would do it all inside Notepad++ (open the file being edited in editor1 and the control file in editor2, and use the editor.* API hooks for all file access), or do it all outside Notepad++ (using the language’s I/O procedures for both the control file and the file being edited). Doing a mix of the two increases the chances that the wrong access will be applied to one of the files, making the script not do what you want; also, it makes the code harder to read, so when you’re trying to figure out what you were doing in 6 months or 6 years, you won’t understand.

                                          A 1 Reply Last reply Apr 16, 2020, 1:44 PM Reply Quote 2
                                          9 out of 27
                                          • First post
                                            9/27
                                            Last post
                                          The Community of users of the Notepad++ text editor.
                                          Powered by NodeBB | Contributors