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.
    • Alan KilbornA
      Alan Kilborn @Bert Barber
      last edited by Alan Kilborn

      @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++?

      Bert BarberB 1 Reply Last reply Reply Quote 1
      • Bert BarberB
        Bert Barber @Alan Kilborn
        last edited by

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

        Alan KilbornA 2 Replies Last reply Reply Quote 0
        • PeterJonesP
          PeterJones
          last edited by

          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.

          Bert BarberB 1 Reply Last reply Reply Quote 1
          • Alan KilbornA
            Alan Kilborn @Bert Barber
            last edited by Alan Kilborn

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

            Alan KilbornA 1 Reply Last reply Reply Quote 1
            • Alan KilbornA
              Alan Kilborn @Alan Kilborn
              last edited by Alan Kilborn

              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!

              Bert BarberB Alan KilbornA 2 Replies Last reply Reply Quote 1
              • Alan KilbornA
                Alan Kilborn @Bert Barber
                last edited by

                @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
                • Bert BarberB
                  Bert Barber @Alan Kilborn
                  last edited by

                  @Alan-Kilborn Thanks!

                  1 Reply Last reply Reply Quote 1
                  • Bert BarberB
                    Bert Barber @PeterJones
                    last edited by

                    @PeterJones Thanks for your reply!

                    1 Reply Last reply Reply Quote 1
                    • Alan KilbornA
                      Alan Kilborn @Alan Kilborn
                      last edited by

                      @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
                      • EkopalypseE
                        Ekopalypse
                        last edited by Ekopalypse

                        @Bert-Barber

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

                        Alan KilbornA 1 Reply Last reply Reply Quote 1
                        • Alan KilbornA
                          Alan Kilborn @Ekopalypse
                          last edited by

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

                          Bert BarberB 1 Reply Last reply Reply Quote 1
                          • EkopalypseE
                            Ekopalypse
                            last edited by

                            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 …

                            Alan KilbornA 1 Reply Last reply Reply Quote 0
                            • Alan KilbornA
                              Alan Kilborn @Ekopalypse
                              last edited by Alan Kilborn

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

                              Bert BarberB EkopalypseE 2 Replies Last reply Reply Quote 1
                              • Bert BarberB
                                Bert Barber @Alan Kilborn
                                last edited by

                                @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
                                • Bert BarberB
                                  Bert Barber @Alan Kilborn
                                  last edited by

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

                                  EkopalypseE 1 Reply Last reply Reply Quote 0
                                  • EkopalypseE
                                    Ekopalypse @Alan Kilborn
                                    last edited by

                                    @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
                                    • EkopalypseE
                                      Ekopalypse @Bert Barber
                                      last edited by

                                      @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
                                      • Alan KilbornA
                                        Alan Kilborn
                                        last edited by

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

                                        PeterJonesP 1 Reply Last reply Reply Quote 2
                                        • PeterJonesP
                                          PeterJones @Alan Kilborn
                                          last edited by

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

                                          Alan KilbornA 1 Reply Last reply Reply Quote 2
                                          • EkopalypseE
                                            Ekopalypse
                                            last edited by

                                            Ok - now I see where my mistake is.
                                            I did not understand that there are two files involved.
                                            Even after the discussion with Alan I was still thinking
                                            there is only one file. Doh.

                                            @Thx @PeterJones for clarifying and
                                            sorry @Bert-Barber and @Alan-Kilborn for the confusion.

                                            Now the code makes much more sense.

                                            So the important questions now is, which one is better.
                                            Charcoal or propane grill? Just kidding :-D

                                            Alan KilbornA 1 Reply Last reply Reply Quote 2
                                            • First post
                                              Last post
                                            The Community of users of the Notepad++ text editor.
                                            Powered by NodeBB | Contributors