Massive list and massive search and replace?
-
@Alan-Kilborn Thanks, but wow, this seems a bit too complicated for me.
Is it possible to simplify it just to read an Excel page, find A2 replace it with B2 for every text files are opened in NPP?I can just add new words in the excel file, then I just run the script
-
Most of the complication is just setting up Python Script plugin and installing the script once.
After that, you just have to run the script when you have a file
blue->orange replace this->with this the delimiter is->a minus followed by a greater than
It’s really not hard to create that substitution file.
And having the replacement data in Excel would make it harder for Alan to write a script for you (and this forum is not a code-writing service), but because the script would still be written in PythonScript, you would still have to install PythonScript and install that script once. Running it would be just as easy for you whether you run it with a text file as the source of the search->replace pairs or whether you run a script that has to parse some external Excel spreadsheet (easier, actually, for the text file, because then you don’t have to also run Excel just to prepare for a search-and-replace in Notepad++). He would still have to have all those confirmation dialogs whether the map is in Excel or in Notepad++… and he’d also have to have another dialog which asks you where the Excel spreadsheet was.
He wrote this not just for you, but also for all the other people who ask for nearly the same functionality (we’ve seen similar requests a lot over the years, and he finally decided that we needed one generic script to handle them all, so that we’d stop having to write customized scripts for each user). If using this generic script is too complicated for you, you will not like any implementation that anyone here is able to give you.
Good luck.
-
@Calvin-Foo said in Massive list and massive search and replace?:
Is it possible to simplify it just to read an Excel page, find A2 replace it with B2 for every text files are opened in NPP?
If you are referring to an Excel file with extension XLSX then no, Notepad++ does NOT read files which are binary in nature very well. It is after all a TEXT editor, not a Binary editor.
However since you refer to an “Excel page” and refer to words in 2 columns, that could also be a CSV (comma separated value) file. And whilst @Alan-Kilborn has used a TSV (tab separated value) file, the 2 are very similar. He possibly could alter his code to use the comma instead of a tab, but possibly used the tab to prevent possible confusion within words.
But doing that minor change to his code isn’t going to simplify the process anyways. Just be thankful he has gone to such lengths to help you. Sometimes doing processes such as you outlined can be made easier, but will still require time to setup.
Terry
PS I see @PeterJones has stated the same.
-
@Terry-R said in Massive list and massive search and replace?:
And whilst @Alan-Kilborn has used a TSV (tab separated value) file
Actually it isn’t a “tab”, although I can see why you’d think that. I chose
-
followed by>
as the delimiter. The delimiter is specifically variable-ized in the script, so one could easily change it to whatever is desired. -
@Calvin-Foo said in Massive list and massive search and replace?:
Is it possible to simplify it just to read an Excel page, find A2 replace it with B2 for every text files are opened in NPP?
I suppose it IS possible, but not by me.
The intent of the script was to solve kind of a general case problem, in a general way.
Of course anyone can treat it as a demo, and feel free to modify it at will. -
I guess I need to learn from ground up. I only have experience in writing ASP 3.0. and SQL Server.
I guess I just start from there
Maybe anyone can give me a simple guide on How to write a simple replace text script? I’ll further study from there and include a list of text (maybe import from CSV)
-
There’s very, very, very little to learn. First, follow the instructions in Alan’s second post to you (“REFERENCE” link) LIKE A MONKEY.
Here’s a first script you can run:
#! python import sys print "Old style print syntax" sys.stdout.write("Calvin-Foo's first script -- hello from Python %s\n" % (sys.version,))
You don’t need to know what any of the lines mean or what they do. (Once you get it running, you can hack at it for fun).
There’s a lot complexity in airplanes and elevators and keyboards and phones that you don’t see and don’t need to deal with. It’s really quite similar.
-
@Calvin-Foo said in Massive list and massive search and replace?:
I guess I need to learn from ground up.
I guess I just start from thereI’ll further study from there and include a list of text (maybe import from CSV)In case it isn’t obvious, you could take YOUR data, in whatever (textual) format, and use Notepad++ to change it with a replacement operation into MY demo format, and then just run with the demo solution.
This avoids programming and could (probably) be made into a N++ macro for easy repetitive running.
As an example, take your original problem statement data (I know it isn’t your real data, but we have none of that here, so…):
1. james - James 2. calvin - Calvin 3. new york - New York
You could change that into the needed input format for the script by this operation:
Find:
(?-s)^\d+\. (.+?) - (.+)
Replace:${1}->${2}
Search mode:Regular expression
Wrap around: Checked
Action:Replace All
buttonAfter the replace-all, your data would then look like this:
james->James calvin->Calvin new york->New York
which would be a direct feed-in to the demo script.
this seems a bit too complicated for me
Of course, this data transform may involve learning some regular expressions (don’t know your expertise), but I don’t think it is too much to ask people that request a moderately-complex solution to a problem to do some sort of learning of their own along the way.
How to write a simple replace text script?
Well about the simplest one I can think of would be a one-liner:
editor.replace('apple', 'Apple')
-
-
-
-
-
this script is FANTASTIC.
i do have a question though- in the final output it prints how many replacements were made, but is there any way to see what the actual replacements were?
looking into the code i see
“”" if self.num_repl_made_in_this_file > 0:
self.print('replacing "{fw}" with "{rw}" {n} times'.format( fw=find_what, rw=replace_with, n=self.num_repl_made_in_this_file)) """
but i dont see where that would get printed- it doesnt show up in the python console either
again huge thanks for this one
-
@nerdyone255 said :
this script is FANTASTIC.
Well…glad you like it.
but i dont see where that would get printed- it doesnt show up in the python console either
The
self.print()
function calls are really meant as debug helpers while testing the script. Thus, in the version of the script above, they don’t do anything because thedebug
variable is set to False. If you change the0
to a1
in this line:self.debug = True if 0 else False
or simply change it to:
self.debug = True
then the output of the self.print() calls will go to the PythonScript console window. You’ll see the output you indicated you were interested, plus output from other things that happen while the script is running.
-
@Alan-Kilborn perfect!
-