1

Py File: named Portuguesetranslator.py

with open('C:/Users/User/Desktop/Portuguesetranslator.txt') as f:
    for l in f:
        s = l.split('*')
        editor.replace(s[0],s[1])

I created that into Python Script inside of Notepad++

Then i have my "database" called Portuguesetranslator.txt

And is separated into

Result*Resultado* Event*Evento* .... and more 1k++ exapmles like this

Then the process i do is.. I open a 3rd tab... copy an text from the internet.. and place into that tab.. then i run the script by pressing plugin/python script/portuguesetranslator

And its run in my entire document and search and replace ..

So what im doing wrong?

heltonbiker
  • 26,657
  • 28
  • 137
  • 252
Renan Cidale
  • 842
  • 2
  • 10
  • 23

2 Answers2

1

Try regular expressions. \b is the word boundary command in a regular expression. It means that at that point in the regular expression, you must be on a word boundary (not in the middle of a word). You can wrap this around your s[0]:

import re
with open('C:/Users/User/Desktop/Portuguesetranslator.txt') as f:
    for l in f:
        s = l.split('*')
        editor = re.sub(r'\b' + s[0] + r'\b', s[1], editor)

Edit - for notepad++, it looks like you want the last line to be this:

editor.pyreplace(r'\b' + s[0] + r'\b', s[1])
JoshG79
  • 1,685
  • 11
  • 14
  • so i just need to copy that script and replace for mine?.. and for the database i keep writing with the same model? Result*Resultado* ? – Renan Cidale Jul 18 '13 at 19:32
  • so .. i replaced.. and tried to run the script.. but it doesnt translate anything.. – Renan Cidale Jul 18 '13 at 20:10
  • in the text file .. how the words must be placed?? result*resultado* ? – Renan Cidale Jul 18 '13 at 20:13
  • import re with open('C:/Users/User/Desktop/Portuguesetranslator.txt') as f: for l in f: s = l.split('*') re.sub(r'\b' + s[0] + r'\b', s[1], editor) where is everythihng that it is in the script file – Renan Cidale Jul 18 '13 at 20:14
  • Yes .. its my whole script.. i run that script in my text file .. that script its based in the database founded in C:/Users/User/Desktop/Portuguesetranslator.txt as you can see in the script – Renan Cidale Jul 18 '13 at 20:20
  • is there any way that i can have your skype.. or something.. so i can show my files.. because this comment system its impossible to write .. i tried to replace what you just sent to me.. but still.. when i run that script again.. nothing happens.. i dont know if the problem is in the Portuguesetranslator.txt file.. – Renan Cidale Jul 18 '13 at 20:38
  • btw.. what the translated_file.txt means? im not understanding this code you are saying.. and another thing.. when i use my current code.. to search and replace.. he does the things very well.. but the problem is when i face for example.. "Result" or "Event" because the replace would be "Resultado" and "Evento".. wich means that he ill replace for Resultado .. all good.. but if i run the script one more time.. the ill have the result of "Resultadoado".. because he found "Result" again in the text.. i hope i could make myself clear – Renan Cidale Jul 18 '13 at 20:42
  • remember that.. i do want to translate.. but only following the sentences im setting as the example.. thats why i have the Portuguesetranslator.txt file.. thats why im using the search and replace function.. and my old script.. i just need to copy the text i want to search and replace.. place into notepad++ then run the script.. by plugin/pythonscript/portuguesetranslator.py .. then the script runs into all the text.. and do the magic ... – Renan Cidale Jul 18 '13 at 20:51
  • Ill Edit my post.. take a look please – Renan Cidale Jul 18 '13 at 20:52
  • http://stackoverflow.com/questions/11389466/multiple-word-search-and-replace-in-notepad Im using the script that you see in that example.. i copied.. – Renan Cidale Jul 18 '13 at 21:01
  • @RenanCidale please don't write so many comments, we're not having time to think :P – heltonbiker Jul 18 '13 at 21:04
  • @Renan - I had a bug in my original code. Can you try the original version again? Other than that, I think it's a notepad++ plugin issue, of which I'm totally unfamiliar. – JoshG79 Jul 18 '13 at 21:04
  • @heltonbiker Por favor me ajuda ! – Renan Cidale Jul 18 '13 at 21:07
  • that thing u just updated starts a loop.. and closes the notepad++ – Renan Cidale Jul 18 '13 at 21:12
0

The answer from @JoshG79 didn't quite work for me (N++ 7.5.1), only the last line needed to be modified:

with open('C:\Users\Administrator\Desktop\IPL\WIP\EnergyTagSubstitutions.txt') as f: for l in f: s = l.split() editor.rereplace(r'\b' + s[0] + r'\b', s[1])

reubenb87
  • 303
  • 4
  • 11