Search and Replace



  • I have created a space delimited file with two columns (*.prn)

    fe5b1c14-5743-4163-ac73-9e793b72fd90-image.png

    For every instance (find) of the text in column 1, I want to replace it with the text in column 2. The file I am searching has over 9,000 lines.

    I have the following in a Python script I am running. Really strange, NONE of the words are being replaced but some of the ids (ending with “__” ) are being replaced.

    with open(‘D:/Note Pad Plus Macros/Conversion Data.prn’) as f:
    for l in f:
    s = l.split()
    editor.replace(r’\b’ + s[0] + r’\b’, s[1])

    Any suggestions would be greatly appreciated. I have over 1,000 more changes to make and cutting and pasting is getting very tiring.

    Thanks so very much in advance for any assistance…



  • @Bert-Barber

    One problem I see immediately is that your data has spaces inside each column, so your columns can’t be “space delimited”. Well, they can be, but nothing will be able to tell a space between the columns from a space within a column.

    For the first example of this, see the Aged Out in column 1.

    You have to solve this problem before you can make any progress on this.

    Also, not sure how you are showing us the data, Excel? If so, how does that relate to what the same data looks like (and actually IS) in Notepad++?



  • @Alan-Kilborn Thank you for your quick response. Can the code:

    with open(‘D:/Note Pad Plus Macros/Conversion Data.prn’) as f:
    for l in f:
    s = l.split()
    editor.replace(r’\b’ + s[0] + r’\b’, s[1])

    be changed to work with a comma delimited file?

    The data shown is a screen capture of excel file.



  • Python 2.7 split documentation will help you figure out what argument to pass to split to make it split on commas instead of whitespace.

    Please note that split-based parsing of CSV is a poor substitute for a full CSV library, because while splitting on commas can handle the simple forms of CSV, documents that have commas or newlines embedded in cells will confuse all but a fully-vetted library.



  • @Bert-Barber

    Can the code:
    be changed to work with a comma delimited file?

    If there is going to be no commas in your data except the one which separates the columns, then yes, try:

    s = l.split(',')

    Also, I’d remove the r'\b' stuff. You don’t need it, and it won’t work with editor.replace anyway (you’d need editor.rereplace). But again, you don’t need it.



  • Maybe in your generated lookup data file do a file search for (?-s),.*?, and see if you get any hits. If you don’t you’ll know your data only has one comma per line (the one from the export process) and you are good to go with a simple “split” based scheme. Otherwise, yeah, Peter is right in that it gets way more complicated. But I’d think by now you are fairly close to the final solution. Nice to see someone other than the regulars here do something with Python!



  • @Bert-Barber

    If you hadn’t gone with a Python solution, a good reference for a table-driven replacement is HERE.



  • @Alan-Kilborn Thanks!



  • @PeterJones Thanks for your reply!



  • @Alan-Kilborn said in Search and Replace:

    in your generated lookup data file do a file search for (?-s),.*?, and see if you get any hits

    Hopefully it is obvious but that would be a search with Search mode set to Regular Expression



  • @Bert-Barber

    maybe I’m missing something but you CANNOT use editor.replace on a file.



  • @Ekopalypse said in Search and Replace:

    maybe I’m missing something

    I took it as the OP has the file they are applying the lookup-replacement on active in Notepad++ when they run the script. We’ve lost the indentation for the OP’s script because of they posting method used, but if it is indented the way I would think, I believe the (rough) logic works?



  • Hmm … strange way, … and potentially dangerous.
    You open the file and do the replace in the scintilla buffer.
    As long as it is not saved while still open …



  • @Ekopalypse said in Search and Replace:

    You open the file and do the replace in the scintilla buffer.

    My understanding of OP’s scenario is:

    • file to be modified is the active Notepad++ tab
    • the script uses non-Notepad++ Python code to open and read the file that contains the lookup table (this file is NOT currently open in Notepad++)
    • script used Notepad++ object to modify the Scintilla buffer of the active file

    I don’t see any issue whatsoever with that process. Of course it is always good to have a backup copy of data until you verify that things are correct.

    Of course one could argue “why not do it all in straight Python and avoid Notepad++ entirely”. And that’s a perfectly valid route as well.



  • @Alan-Kilborn Yes the file I am applying the lookup to is active in Notepad ++.

    I do sincerely appreciate the help!



  • @Alan-Kilborn You understanding or the process I am attempting is exactly correct. I am experimenting now and will post response shortly.

    Once again I do sincerely appreciate the suggestions and recommendations, I am learning!



  • @Alan-Kilborn said in Search and Replace:

    I don’t see any issue whatsoever with that process.

    If I’m allowed to use an analogy.
    Of course I can use a gas grill and put coal in it to make my barbecue,
    but should I?
    The result could be unforeseen.
    What if the OP is under the impression that this is the correct way of doing this kind of stuff?



  • @Bert-Barber said in Search and Replace:

    I am learning

    If your intention is to use the PythonScript plugin and a python script
    to manipulate a file then the “correct” way of doing this is either
    to use

    with open('FILENAME','MODE') as f:
        x = f.read() or f.write() etc.
    

    if you are not loading it into npp,
    but if you have it open in npp and want to see the results
    you normally do something like

    lines = editor.getText().splitlines()
    for line in lines:
        print(line.upper())
    


  • @Ekopalypse

    Analogy noted, I think I did that once, with an old gas grill I was about ready to discard. Or I thought about it.

    Back to code, I still see absolutely nothing wrong.
    In fact, it may be the cleanest and shortest way for the OP to achieve his goal (with code).
    Is it the way I would have done it? Probably not.

    The bottom line is we must agree to disagree, or one of us is still missing something.
    Perhaps @PeterJones could offer an opinion to perhaps “turn on the lights” on this.



  • @Alan-Kilborn & @Ekopalypse ,

    I doubt I’ll “turn the lights on”.

    The OP claims to be using the procedure Alan outlined (reading an external file to manipulate how the internal Notepad++/Scintilla file is being edited). As such, I don’t think the code isn’t necessarily bad (ie, OP hasn’t started dumping propane in the charcoal grill), but it’s not the best (why put charcoal in your broken gas grill – even if the propane isn’t connected – when you have a made-for-the-task charcoal grill on the porch already? If your excuse is that no one told you the black R2D2-looking thing on the porch was actually a charcoal grill, well, now you know.).

    Personally, I would do it all inside Notepad++ (open the file being edited in editor1 and the control file in editor2, and use the editor.* API hooks for all file access), or do it all outside Notepad++ (using the language’s I/O procedures for both the control file and the file being edited). Doing a mix of the two increases the chances that the wrong access will be applied to one of the files, making the script not do what you want; also, it makes the code harder to read, so when you’re trying to figure out what you were doing in 6 months or 6 years, you won’t understand.