简体   繁体   中英

Multiple word search and replace in notepad++ using a python script

I usually use a python script to replace several expressions at once in a txt file.

I create a reference file with my expressions to be replaced, the expressions being separated with space:

value1 valueA
value2 valueB
value3 valueC

then I use a python script:

with open('path to my referencefile.txt') as f:
    for l in f:
        s = l.split()
        editor.replace(s[0], s[1])

It normally works but I have an issue when I try to replace expressions which already exist and that I want to re-order basically.

Example:

value1 value2
value2 value3
value3 value4

In the end value1 will be replaced by value2 then value2 will be replaced by value3. Finally, value3 will be replaced by value4. Which means value1 became value4 instead of value2... How do I keep value1 replaced by value2 instead of this cascade of replacements? (and of course value2 replaced by value3 and value3 by value4)

In notepad++ it's possible using the regex replace function with the formula (value1)|(value2)|(value3) replaced by (?1value2)(?2value3)(?3value4). Is there a way to achieve the same result with the python script mentioned above?

Also, how do you modify the python script to replace expressions only in the selected area of a txt file?

Thanks for your help :)

I have not fully understood the question, but you can use this code.

with open("val.txt", "r+") as f:
file = f.readlines()
f.seek(0)
for line in file:
    s = line.split()
    s.reverse()
    f.writelines(s[0] + ' ' + s[1] + '\n')

This simply changes the two columns. However, I think it is the right way to treat the value of the txt file.

if you want to replace those two columns in another text file then you can use below code.

with open('path to my referencefile.txt') as f:
    another_file = open("another file name","w")
    for l in f:
        s = l.split()
        another_file.write(s[0] + " " + s[0][:-1] + str(int(s[0][-1])+1) + "\n" )
    another_file.close()

As you can see, here we are replacing ( not exactly replacing but coping) the 'A' to '2' in the very first line of the input text file and so on, simultaneously writing down to another file.

If the above code solve the purpose than it fine, else let me know your doubt.

Sample script Multiples_SR.py from Python Script plugin sample scripts collection does what you need (see origin here ).

From the explanation of the script:

This script :

Reads an existing "SR_List.txt" file, of the CURRENT directory, containing a list of SEARCH/REPLACEMENT strings, ONE PER line Selects, one at a time, a COUPLE of SEARCH and REPLACEMENT regexes/expressions/strings/characters Executes this present S/R on CURRENT edited file, in NOTEPAD++ Loop till the END of the file Any PURE BLANK line or COMMENT line, beginning with '#', of the "SR_list.txt" file, is simply IGNORED

For EACH line, in the "SR_List.txt" file, the format is

<DELIMITER><SEARCH regex><DELIMITER><REPLACE regex><DELIMITER>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM