Good morning.
I am not real savvy with field calculator. We have a couple fields that contain service address information. Someone opted to add additional information enclosed by parentheses in this field. I need to remove the parentheses and data within. Any advice or suggestions appreciated.
Thanks
Terry
yes, I used to use them, but they changed in python 3.x so I just lost track of what applied to where
Dan,
Actually James's regex gets all the string(s) between the ()s. RegEx is very powerful for stuff like this.
you missed the bit I did earlier... the stuff between the braces needs to be removed as well
Using the Python interpreter, import the regular expression module (re) in the pre-logic script code block (click on Show Codeblock to open this):
import re<SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
Then use a substitution along the lines of this:
re.sub(r"\([^\)]+\)", "", !ServiceAddr!)<SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
Should work fine with multiple pairs of parentheses but will likely need to be tweaked if you've got nested pairs.
well the other function you might use.... but
Keep between the brackets
fld <SPAN class="operator token">=</SPAN> <SPAN class="string token">"some stuff (the stuff) with more"</SPAN> fld<SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">")"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"("</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># yields</SPAN> <SPAN class="string token">'the stuff'</SPAN> <SPAN class="comment token"># just replace fld with !YourFieldName!</SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Dump between the brackets
fld <SPAN class="operator token">=</SPAN> <SPAN class="string token">"some stuff (the stuff) with more"</SPAN> fld<SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"("</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>strip<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> fld<SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">")"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="string token">'some stuff with more'</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
ahh missed that bit
Thank you Dan.
What happened is when the service address got populated, someone added additional info between the parentheses in the same field, rather than using an address2 field
I don't need to save that info, just remove it.
Thank you
Thank you Robert. I'll give it a shot.
What about the data within?
I need to remove the parentheses and data within
Terry a little function for the field calculator
Python parser of course
expression box .... brace(!YourFieldNameHere!)
code block
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">brace</SPAN><SPAN class="punctuation token">(</SPAN>fld<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"""replace brackets"""</SPAN> a <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'['</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">']'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'{'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'}'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'('</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">')'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> i <SPAN class="keyword token">in</SPAN> a<SPAN class="punctuation token">:</SPAN> fld <SPAN class="operator token">=</SPAN> fld<SPAN class="punctuation token">.</SPAN>replace<SPAN class="punctuation token">(</SPAN>i<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN> fld<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
of course you are putting the result in a new text field of appropriate width
Terry,
Using VBScript in the field calculator you just use Mid([yourfieldname], 1, (InStr([yourfieldname],"(") - 1))
This will look in the thread for the first occurance of "(" and take everything before the "(" and use it for the field calc.
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.