Search and Replace
-
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 usewith 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 likelines = editor.getText().splitlines() for line in lines: print(line.upper())
-
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. -
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 ineditor2
, and use theeditor.*
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. -
Ok - now I see where my mistake is.
I did not understand that there are two files involved.
Even after the discussion with Alan I was still thinking
there is only one file. Doh.@Thx @PeterJones for clarifying and
sorry @Bert-Barber and @Alan-Kilborn for the confusion.Now the code makes much more sense.
So the important questions now is, which one is better.
Charcoal or propane grill? Just kidding :-D -
@PeterJones said in Search and Replace:
I doubt I’ll “turn the lights on”
Not a bad try though!
The “two-view”
editor1
andeditor2
thing does make a lot of sense for this application. I hadn’t thought of that. For the OP’s benefit, and what Peter didn’t explicitly say, is that you can access Pythonscript functions specific to an editor, like this:editor1.replace()
oreditor2.replace()
.Where it gets a little dicey is, how do you know which view on your screen contains which data (the data to be acted on or the lookup table data). Intuitively, one thinks that the view on the left is
editor1
and the view on the right iseditor2
, but it is possible for that to be reversed. Thus, be careful! Side note: If you close one of the views, don’t assume that the one remaining iseditor1
.Doing a mix of the two increases the chances that the wrong access will be applied to one of the files
Hmmm, kinda like what I just mentioned about mixing up the views.
I think it is getting bogged down because both of the data are text. If the lookup table was coming from a database, no one would have any complaint about the OP’s method [ or start talking about BBQ grills – :-) ].
it makes the code harder to read
Not a strong argument, but I’ll give it to you. :-)
so when you’re trying to figure out what you were doing in 6 months or 6 years
Probably the OP does this once and then throws it away.
Even after a long time, I think the code would make sense. -
@Ekopalypse said in Search and Replace:
I did not understand that there are two files involved.
Even after the discussion with Alan I was still thinking there is only one file.Yea, with these kind of table-lookup-replacement problems we usually end up with a regex solution where the table is appended below the actual data…in ONE file. Maybe that sent your thinking this way.
Since the OP mentioned Python before @guy038 could jump in with regex, though, it went a different way.
Charcoal or propane grill?
There is no debate possible here:
CHARCOAL
-
Because I can only upvote once, this is +1 for charcoal :-D
-
I am learning so, I certainly do not object to being told the simplest way to accomplish my task. At this time I have the following:
I have the following as an excel csv file:
the sample code I am experimenting with is:
And the script I am running is:
The results I am getting is:
So, it seems to be working except for the extra spaces and lines that are being added. What am I doing wrong that is causing the extra space to be added?
Thanks so very much in advance for your assistance!!!
-
@Bert-Barber said in Search and Replace:
working except for the extra spaces and lines that are being added.
I believe the extra lines are coming because
l
has the newline from the CSV still embedded, and when you split it,s[1]
will contain the newline… You need to remove the newline before doing the split, such ass = l.rstrip().split(',')
.As far as the extra spaces: you haven’t shown us the actual CSV, only the spreadsheet-interpretation of the CSV… but it may be that in the raw CSV, it’s something like
LEFT COLUMN, RIGHT COLUMN AFTER ONE OR MORE SPACES
The split would then leave all those spaces in
s[1]
. If you don’t want those spaces, you could strip all leading and trailing spaces off of a string using.strip()
:editor.rereplace(s[0], s[1].strip() )
-
@PeterJones YOU ARE AWESOME - THANK YOU SO VERY MUCH!!!