• Login
Community
  • Login

PythonScript CSV Sort Help

Scheduled Pinned Locked Moved Notepad++ & Plugin Development
17 Posts 4 Posters 1.1k 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.
  • A
    Alan Kilborn @John Doe 1
    last edited by Apr 13, 2022, 2:43 PM

    @john-doe-1

    If it were me I might try a single editor call instead of multiple:

    editor.setText('\r\n'.join(lines_list))
    
    J 1 Reply Last reply Apr 13, 2022, 2:57 PM Reply Quote 2
    • J
      John Doe 1 @Alan Kilborn
      last edited by Apr 13, 2022, 2:57 PM

      @alan-kilborn Thanks for the help again Alan (my apologies for straying off topic in my previous post), by this do you mean rather than clearing all the text separately and using a loop to add the new text to use the function you suggested?

      A 1 Reply Last reply Apr 13, 2022, 3:01 PM Reply Quote 0
      • A
        Alan Kilborn @John Doe 1
        last edited by Apr 13, 2022, 3:01 PM

        @john-doe-1

        No apology needed for straying off-topic, we’ll steer you back on.

        So it appears you aren’t using pandas now, in favor of “Python’s csv library”? Again, specifics of that are off-topic as well…

        But manipulating via editor, now that’s ON-TOPIC.

        do you mean rather than clearing all the text separately and using a loop to add the new text to use the function you suggested?

        Yes.

        J 1 Reply Last reply Apr 13, 2022, 3:12 PM Reply Quote 1
        • J
          John Doe 1 @Alan Kilborn
          last edited by Apr 13, 2022, 3:12 PM

          @alan-kilborn Okay I gave it a shot, and I ran into this error:

          editor.setText(“\r\n”.join(sorted))
          TypeError: sequence item 0: expected string, list found

          Here is the initialization of sorted:
          sorted = sorted(csv, key=operator.itemgetter(item), reverse=order)

          A 1 Reply Last reply Apr 13, 2022, 3:25 PM Reply Quote 0
          • A
            Alan Kilborn @John Doe 1
            last edited by Alan Kilborn Apr 13, 2022, 3:27 PM Apr 13, 2022, 3:25 PM

            @john-doe-1 said in PythonScript CSV Sort Help:

            I ran into this error

            Hard to say… Your sorted variable must be a list of strings.

            Here’s a fully working example of my snippet from before:

            lines_list = [ 'hello', 'there', 'world' ]
            editor.setText('\r\n'.join(lines_list))
            
            J 2 Replies Last reply Apr 13, 2022, 4:34 PM Reply Quote 1
            • J
              John Doe 1 @Alan Kilborn
              last edited by Apr 13, 2022, 4:34 PM

              This post is deleted!
              P 1 Reply Last reply Apr 13, 2022, 5:48 PM Reply Quote 0
              • P
                PeterJones @John Doe 1
                last edited by Apr 13, 2022, 5:48 PM

                @john-doe-1 ,

                My sorted variable is a list of lists of strings. To try and explain it a bit, it is a list of CSV rows (which themselves are lists of strings, those strings being individual fields in the CSV. It’s a bit hard to put into words, does that make sense?

                A list of a list of strings is not the same as a list of strings. For each row of the CSV, you will have to join the elements of the row into a single string (join with commas); then that list of one string per row needs to be joined down into a single massive string (join with newlines, as Alan showed).

                And you are straying into basic Python questions again. This forum is for Notepad++.

                So, if you want to ask about any of the editor object methods or notepad object methods that PythonScript plugin makes available, this is a fine place to do that (so editor.setText() is completely on topic). Or if you want to ask “how do I find the line number for the last line in the active file”, then the answer will be one of the editor methods; or if you want to ask about how to run a specific menu command, we would point you to one of the notepad methods. But if you want to ask about “how do I join an array of array of strings down into an array of single strings, and further down into one massive string,” that is off topic for this forum, and should be asked somewhere where they talk about python specifically, or where they have a wider on-topic for any programming questions.

                (deleting it after I start replying won’t stop me from replying)

                J 1 Reply Last reply Apr 13, 2022, 5:49 PM Reply Quote 1
                • J
                  John Doe 1 @Alan Kilborn
                  last edited by Apr 13, 2022, 5:48 PM

                  @alan-kilborn said in PythonScript CSV Sort Help:

                  Hard to say… Your sorted variable must be a list of strings.

                  sorted = sorted(csv, key=operator.itemgetter(item), reverse=order)

                  The sorted function makes it so that sorted ends up being a list of lists of strings.

                  1 Reply Last reply Reply Quote 0
                  • J
                    John Doe 1 @PeterJones
                    last edited by Apr 13, 2022, 5:49 PM

                    @peterjones said in PythonScript CSV Sort Help:

                    (deleting it after I start replying won’t stop me from replying)

                    Was trying to rephrase my question by quoting what I was replying to, sorry about that.

                    P 1 Reply Last reply Apr 13, 2022, 6:01 PM Reply Quote 0
                    • P
                      PeterJones @John Doe 1
                      last edited by PeterJones Apr 13, 2022, 6:04 PM Apr 13, 2022, 6:01 PM

                      @john-doe-1 said in PythonScript CSV Sort Help:

                      Was trying to rephrase my question by quoting what I was replying to, sorry about that.

                      I am betting that your first phrasing, when you said it was a “list of lists of strings” is more accurate – but that is off topic here anyway.

                      Someplace else, you need to figure out how to convert your csv item into one monolithic string, with all the commas and newlines – this forum is not the place to flesh that out, since that’s generic python stuff, and a matter of knowing your data. Once it is a monolithic string, the on-topic command editor.setText(str) will work to set the text of the entire document to that massive string. If you need help generating that massive string, find a Python or Programming forum. If you need help with editor or notepad object methods, this is the right place.

                      J 1 Reply Last reply Apr 13, 2022, 6:26 PM Reply Quote 1
                      • J
                        John Doe 1 @PeterJones
                        last edited by Apr 13, 2022, 6:26 PM

                        @peterjones Okay, sorry about that again and thank you for trying to clear things up anyways : - )

                        A 1 Reply Last reply Apr 13, 2022, 8:52 PM Reply Quote 0
                        • A
                          Alan Kilborn @John Doe 1
                          last edited by Alan Kilborn Apr 13, 2022, 8:54 PM Apr 13, 2022, 8:52 PM

                          @john-doe-1

                          Getting back to this…the point I was trying to make was that if you have some “data preparation” scripting, you are best off if you do all of that manipulation purely in Python functions and then make very few editor.xxx() calls.

                          Why? Well, calling editor.xxx() functions can get “expensive” in terms of time (which was the original complaint in this thread). And why is this? Because, it typically can force Notepad++ and underlying components to do a lot of useless work. And that eats up the clock.

                          If the screen has to be updated, which is typical of something like editor.addText(), and you’re just going to do another call to that mere milliseconds later (e.g. in a “loop”), it is pointless to update the screen. Save that for ONE call near the end, exactly like I show with .setText() earlier.

                          Make sense? Maybe it complicates your coding, but isn’t that an acceptable trade for runtime performance? Of course I don’t have the same data as you, so I’m only speculating on why it might be “slow”…

                          J 1 Reply Last reply Apr 14, 2022, 2:08 PM Reply Quote 2
                          • J
                            John Doe 1 @Alan Kilborn
                            last edited by Apr 14, 2022, 2:08 PM

                            @alan-kilborn Yes, limiting the number of editor calls (especially in things like nested loops) improved the speed tremendously, thank you very much for your assistance once again! : - )

                            C 1 Reply Last reply Apr 15, 2022, 7:44 AM Reply Quote 2
                            • C
                              chcg @John Doe 1
                              last edited by Apr 15, 2022, 7:44 AM

                              @john-doe-1 Maybe if your script is in a usable state you want to provide it for https://github.com/bruderstein/PythonScript/tree/master/scripts/Samples . Either via PR or by creating a ticket and add it as attachement.

                              A J 2 Replies Last reply Apr 15, 2022, 11:29 AM Reply Quote 1
                              • A
                                Alan Kilborn @chcg
                                last edited by Apr 15, 2022, 11:29 AM

                                @chcg said in PythonScript CSV Sort Help:

                                Maybe if your script is in a usable state you want to provide it for…

                                I’d say if the script uses Pandas, then probably it isn’t a good candidate for this. If it only uses standard Python stuff (that would be included with PythonScript) then sure, why not?

                                1 Reply Last reply Reply Quote 1
                                • J
                                  John Doe 1 @chcg
                                  last edited by Apr 18, 2022, 2:16 PM

                                  @chcg I would say it is pretty far from usable currently, and I have plans to add some libraries to it (including pandas) in the near future, thanks for the suggestion though : - )

                                  1 Reply Last reply Reply Quote 0
                                  11 out of 17
                                  • First post
                                    11/17
                                    Last post
                                  The Community of users of the Notepad++ text editor.
                                  Powered by NodeBB | Contributors