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

              @PeterJones said in Search and Replace:

              I doubt I’ll “turn the lights on”

              Not a bad try though!

              The “two-view” editor1 and editor2 thing does make a lot of sense for this application. I hadn’t thought of that. For the OP’s benefit, and what Peter didn’t explicitly say, is that you can access Pythonscript functions specific to an editor, like this: editor1.replace() or editor2.replace().

              Where it gets a little dicey is, how do you know which view on your screen contains which data (the data to be acted on or the lookup table data). Intuitively, one thinks that the view on the left is editor1 and the view on the right is editor2, but it is possible for that to be reversed. Thus, be careful! Side note: If you close one of the views, don’t assume that the one remaining is editor1.

              Doing a mix of the two increases the chances that the wrong access will be applied to one of the files

              Hmmm, kinda like what I just mentioned about mixing up the views.

              I think it is getting bogged down because both of the data are text. If the lookup table was coming from a database, no one would have any complaint about the OP’s method [ or start talking about BBQ grills – :-) ].

              it makes the code harder to read

              Not a strong argument, but I’ll give it to you. :-)

              so when you’re trying to figure out what you were doing in 6 months or 6 years

              Probably the OP does this once and then throws it away.
              Even after a long time, I think the code would make sense.

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

                @Ekopalypse said in Search and Replace:

                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.

                Yea, with these kind of table-lookup-replacement problems we usually end up with a regex solution where the table is appended below the actual data…in ONE file. Maybe that sent your thinking this way.

                Since the OP mentioned Python before @guy038 could jump in with regex, though, it went a different way.

                Charcoal or propane grill?

                There is no debate possible here:

                CHARCOAL

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

                  Because I can only upvote once, this is +1 for charcoal :-D

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

                    I am learning so, I certainly do not object to being told the simplest way to accomplish my task. At this time I have the following:

                    I have the following as an excel csv file:
                    fb5f4427-6ead-4f1e-b6db-fe5c0ec18d68-image.png

                    the sample code I am experimenting with is:
                    4f4472b6-0e1c-45ab-9ee8-d2f900e35642-image.png

                    And the script I am running is:
                    17d90ea1-fb71-4919-879a-929cfb21c223-image.png

                    The results I am getting is:
                    d2c2c3b6-a95e-4e12-8065-b89b7888e8bf-image.png

                    So, it seems to be working except for the extra spaces and lines that are being added. What am I doing wrong that is causing the extra space to be added?

                    Thanks so very much in advance for your assistance!!!

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

                      @Bert-Barber said in Search and Replace:

                      working except for the extra spaces and lines that are being added.

                      I believe the extra lines are coming because l has the newline from the CSV still embedded, and when you split it, s[1] will contain the newline… You need to remove the newline before doing the split, such as s = l.rstrip().split(',').

                      As far as the extra spaces: you haven’t shown us the actual CSV, only the spreadsheet-interpretation of the CSV… but it may be that in the raw CSV, it’s something like

                      LEFT COLUMN,             RIGHT COLUMN AFTER ONE OR MORE SPACES
                      

                      The split would then leave all those spaces in s[1]. If you don’t want those spaces, you could strip all leading and trailing spaces off of a string using .strip():

                          editor.rereplace(s[0], s[1].strip() )
                      
                      Bert BarberB 1 Reply Last reply Reply Quote 2
                      • Bert BarberB
                        Bert Barber @PeterJones
                        last edited by

                        @PeterJones YOU ARE AWESOME - THANK YOU SO VERY MUCH!!!

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