multiple text replacing with different text
-
@PeterJones said in multiple text replacing with different text:
Python Script
Interesting; when I tried the script I got this error:
return replacementList[counter] if counter < len(replacementList) else m.string <type 'exceptions.RuntimeError'>: This method or property is not supported under Notepad++ PythonScript
I would guess this is related to usage of
m.string
which I have never seen before. Maybe this is only supported in a P.S. that is newer than the one I use: 1.5.4Anyway, I tried out the script because I was going to suggest the following alternative method, and it just so happens that it avoids that error for me as well.
Change the “return” line to:
return replacementList[counter]
and change the “.replace” line to:
editor.replace(keywordText, nextReplacement, 0, 0, editor.getLength(), len(replacementList))
This is instructive because it shows how to use PythonScript code to limit the number of replacements made; in this case, to the length of the replacement list.
-
I am using PythonScript 1.5.4, so that’s not it
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] Initialisation took 750ms Ready.
Oh, wait, my example didn’t try to go beyond, so was never triggering the ELSE. when I add some more USA to the example data, then it does give me the error.
I confirmed in the PythonScript 1.5.4 docs that its MatchObject doesn’t implement the re.MatchObject’s
.string()
method.
However, since the match string will always be identical to
keywordText
(because it’s a text search, not a regex search), if I change that line to
return replacementList[counter] if counter < len(replacementList) else keywordText
… then the error goes away and everything works even with too manyUSA
instances.But yes, I like your idea of using the optional parameters to avoid having to do the conditional return.
-
ah, also, instead of
m.string()
, I can usem.group(0)
– group#0 is the whole match, even when it’s not a grouped or regex-based replacement. -
@PeterJones said in multiple text replacing with different text:
1- have you ever visited the USA
2- the USA Freedom Act.
3- she lives in the USAthanks for the help @PeterJones much much appreciated
I have tried installing the python code without changing any of the text when I tried on this example:1- have you ever visited the USA 2- the USA Freedom Act. 3- she lives in the USA
it does not change anything
-
@Handa-Flocka said in multiple text replacing with different text:
it does not change anything
Can you tell me the exact steps you took, because it works for me.
-
… for example, here’s a screen-capture video:
-
Now it works like a charm!! I forgot some steps during the process
both of them now are working perfectly
it would be preferred to use python script instead of regex in this process if I am dealing with huge data?Thank you for your help so much appreciation man
-
@Handa-Flocka said in multiple text replacing with different text:
it would be preferred to use python script instead of regex in this process if I am dealing with huge data?
Correct. Large amounts of data will mean that the
(.*?~+\s*)
capture groups grows beyond the regex capture-group memory allocated in Notepad++. -
got it!
Thank you and for the effort you put on this -
I am replying to your Chat message here. Questions like this belong in the Forum posts, not in the chat.
Sorry If bothering you
I have tried again to run the same py script on some random code it didn’t want to change
codeEVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift
for example I am selecting word
TYPEE
it doesn’t change at allMy guess is that the chat window got rid of the <open> tags on most of those. But the example included
TYPEE
, and that’s likely all that’s critical for the replacement.- Change
replaceList.py
sokeywordText = 'TYPEE'
; SAVE - Open file with example text
EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift
- Plugins > PythonScript > Scripts >
replaceList
- Result
… SoEVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“CANADA” MODIFIERS=shift
TYPEE
has becomeCANADA
So it works for me.
- Change
-
@PeterJones said in multiple text replacing with different text:
EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift
@PeterJones my bad I thought the thread was locked!
the code works perfectly in one line
but If I tried something like this for exampleEVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift
then it will not work!
-
@Handa-Flocka said in multiple text replacing with different text:
then it will not work!
Sorry, I don’t know what you’re doing differently. With no change compared to my sequence above (that is, with
TYPEE
as the keyword and the same list of three countries as the replacementList), I get:EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“CANADA” MODIFIERS=shift EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“MEXICO” MODIFIERS=shift EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“COSTA RICA” MODIFIERS=shift EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift
Since my list only had 3 countries, the fourth
TYPEE
remains unchanged.Here is my exact
replaceList.py
with your data:# encoding=utf-8 """in response to https://community.notepad-plus-plus.org/topic/21225/ Written by @PeterJones Searches for the value of keywordText multiple times, and replaces it with the next element of replacementList """ from Npp import * keywordText = 'TYPEE' replacementList = [ 'CANADA', 'MEXICO', 'COSTA RICA' ] counter = -1 def nextReplacement(m): global counter counter += 1 return replacementList[counter] if counter < len(replacementList) else m.group(0) editor.beginUndoAction() editor.replace(keywordText, nextReplacement) editor.endUndoAction()
You might want to go to Plugins > PythonScript > Show Console and look for errors. (You can right click in the console window, click Clear, then run the script again on your data file. If anything is printed in the console, it’s an error message.) The error message will likely indicate where you have a typo or syntax error in the script that was introduced when you put in your new
keywordText
andreplacementList
. If you need help with that, copy/paste the errors you see into the forum (as well as your modified copy ofreplaceList.py
) -
This post is deleted! -
This post is deleted! -
@PeterJones After your update it’s a piece of art now!
thanks again