another replacement request for help
-
Thanks and sorry, it was clear in my mind, which is clearly not so clear :P
Here are a couple snapshots:
This is how the data looks. There are hundreds of different UWI’s that I need to replace
And this is what I meant by table replacement. It is simply the old name and the new name separated by a tab
What I need is to go through the file (XML file by the way), find the string on the first column of the table and replace it by the string in the second column of the table.
My issue is that the names (UWI, stands for Unique Well Identifier, which is basically the well name) are unformatted (Different lenghts, multiple characters, spaces, etc)
I hope it is more clear now. Thanks
-
I’m sure the regex gurus here are able to find a solution based on regex
only but I, personally, would use the python script plugin and this scriptfrom Npp import editor1, editor2 replacements = dict(line.split('\t') for line in editor2.getText().splitlines() if line) def replace_with(m): return replacements[m.group()] editor1.rereplace('(?<=<UWI>).+?(?=</UWI>)', replace_with)
Having the data in both views like
and running the script will result in, I hope, the expected result.
-
@Ekopalypse thanks so much!
I wish I could try it, but now I am banging my head to install the plugin, as the firewall is blocking my download… No luck today. -
:-) those network admins … :-)
I don’t know how strict your rules are but maybe a solution would be to download it from github directly? -
@Ekopalypse said in another replacement request for help:
:-) those network admins … :-)
I don’t know how strict your rules are but maybe a solution would be to download it from github directly?Yeah, got it now. Had to write my user and password a hundred times, but it is installed. I am testing it now
-
@Ekopalypse I want to be like you when I grow up :)
It worked like a charm, and faster than any regex I would believe.
Thanks so much
Regards -
I want to be like you when I grow up
LOL - be careful with your wishes sometimes they come true :-D
-
haha thanks.
May I ask another question?
What if I needed to change another field as well?
For example in the image below:
I find the name Aery #B1H in the file but then I need to change the Welltype from Oil to Gas (Or viceversa).
In this case the I would not be replacing the searched word (Aery #B1H), but another field occurring later in the same entry (<Welltype>Oil to Gas)
Thanks again -
I would think this is a find/replace action.
Find:<UWI>Aery #B1H</UWI>\R\h*<WellType>\K.+?(?=</WellType>)
Replace:Gas
tick regular expressionBecause of the usage of the
\K
syntax you can only use replace all button. -
@Ekopalypse said in another replacement request for help:
\R\h
Thanks It works, but I would need to do one by one.
Again I would have a list of different names (Aery #B1H, Aery #D2H, etc) so I was looking for another magic script haha -
but then we would need some kind of mapping again,
otherwise the script does not know when to use what. :-) -
So this is how the xml file looks
<Well replace=“false”>
<UWI>Alexander Gas Unit 1 #2</UWI>
<WellType>Oil</WellType>
<ReserveStatusCollection>
<Working>
<PrimaryProduct>Oil</PrimaryProduct>
<WellResCatCollection>and this is how the replacement file would be
Alexander Gas Unit 1 #2 Oil Gas
Alexander - Wessendorff 1 A3H Oil Gas
Alexander - Wessendorff 1 A4H Oil GasSo the script would look for the first column (Well name), and then replace the word in the second column for the one in the third column:
<WellType>Oil</WellType> to <WellType>Gas</WellType>
Thanks again, you’re saving my life
-
Is the oil column really needed?
I mean, does it needs to be checked or can the script always look for company and then replace whatever is in WellType with the value from the Gas column? -
Edit:
Thinking about, no, no need for that column -
This post is deleted! -
so you can get rid of the second column and the only thing we need to change is the find expression, I guess.
-
from Npp import editor1, editor2 replacements = dict(line.split('\t') for line in editor2.getText().splitlines() if line) def replace_with(m): return replacements[m.group(1)] # search_for = '(?<=<UWI>).+?(?=</UWI>)' search_for = '<UWI>(.+)</UWI>\R\h*<WellType>\K.+?(?=</WellType>)' editor1.rereplace(search_for, replace_with)
-
btw. if you are interested how these regex search work see here for a pretty good description.
-
Thanks again for your help.
For some reason this time it isn’t working.
I will try and fix it. -
Can you open the PythonScript console (plugin->PythonScript->Show Console) to see if there is an error?
The replacement file has company and gas/oil tab separated, correct?