PythonScript CSV Sort Help
-
@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?
-
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.
-
@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 foundHere is the initialization of sorted:
sorted = sorted(csv, key=operator.itemgetter(item), reverse=order) -
@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))
-
This post is deleted! -
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 ornotepad
object methods that PythonScript plugin makes available, this is a fine place to do that (soeditor.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 theeditor
methods; or if you want to ask about how to run a specific menu command, we would point you to one of thenotepad
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)
-
@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.
-
@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.
-
@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 witheditor
ornotepad
object methods, this is the right place. -
@peterjones Okay, sorry about that again and thank you for trying to clear things up anyways : - )
-
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”…
-
@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! : - )
-
@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.
-
@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?
-
@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 : - )