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.
    • John Doe 1J
      John Doe 1 @Alan Kilborn
      last edited by

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

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

        @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))
        
        John Doe 1J 2 Replies Last reply Reply Quote 1
        • John Doe 1J
          John Doe 1 @Alan Kilborn
          last edited by

          This post is deleted!
          PeterJonesP 1 Reply Last reply Reply Quote 0
          • PeterJonesP
            PeterJones @John Doe 1
            last edited by

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

            John Doe 1J 1 Reply Last reply Reply Quote 1
            • John Doe 1J
              John Doe 1 @Alan Kilborn
              last edited by

              @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
              • John Doe 1J
                John Doe 1 @PeterJones
                last edited by

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

                PeterJonesP 1 Reply Last reply Reply Quote 0
                • PeterJonesP
                  PeterJones @John Doe 1
                  last edited by PeterJones

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

                  John Doe 1J 1 Reply Last reply Reply Quote 1
                  • John Doe 1J
                    John Doe 1 @PeterJones
                    last edited by

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

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

                      @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”…

                      John Doe 1J 1 Reply Last reply Reply Quote 2
                      • John Doe 1J
                        John Doe 1 @Alan Kilborn
                        last edited by

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

                        chcgC 1 Reply Last reply Reply Quote 2
                        • chcgC
                          chcg @John Doe 1
                          last edited by

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

                          Alan KilbornA John Doe 1J 2 Replies Last reply Reply Quote 1
                          • Alan KilbornA
                            Alan Kilborn @chcg
                            last edited by

                            @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
                            • John Doe 1J
                              John Doe 1 @chcg
                              last edited by

                              @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
                              • First post
                                Last post
                              The Community of users of the Notepad++ text editor.
                              Powered by NodeBB | Contributors