I need some assistance creating a ArcPy script to field calculate and display the value of a field based on the input of another field. I would like to show the value of attribute NS_Address if the Prefix attribute is N or S and I would like to show the value of attribute EW_Address if the Prefix attribute is E or W.
Make the address field active.
put the function in the code block.
the expression in the expression box
and calculate away
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">my_func</SPAN><SPAN class="punctuation token">(</SPAN>val<SPAN class="punctuation token">,</SPAN> fld1<SPAN class="punctuation token">,</SPAN> fld2<SPAN class="punctuation token">)</SPAN> <SPAN class="string token">""" NS_Address if the Prefix attribute is N or S EW_Address if the Prefix attribute is E or W """</SPAN> <SPAN class="keyword token">if</SPAN> val <SPAN class="keyword token">in</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'N'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'S'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> fld1 <SPAN class="keyword token">elif</SPAN> val <SPAN class="keyword token">in</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'E'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'W'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> fld2 <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> None<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>
expression
my_func(!Prefix!, !NS_Address!, !EW_Address! )
Assumed Answered....
I suppose
Anthony, no problem
You should close the question so others can find the answer
I just had to add a colon after the first line. The expression ran perfectly! Thank you!!!
Dan,
Thanks for the reply. I received the error below when attempting to run that calculation.
Something like this? Untested code...
http://pro.arcgis.com/en/pro-app/arcpy/data-access/updatecursor-class.htmhttps://www.tutorialspoint.com/python/string_split.htm
Python IF...ELIF...ELSE Statements
import arcpy feature_class_path =r"xyz" with arcpy.da.UpdateCursor(feature_class_path, ["NS_Address", "Address"]) as cursor: for row in cursor: row_parts = row.split(" ") prefix = "" if row_parts[0] == "N" or row_parts[0] == "S": prefix = "NS_Address" else: prefix = "EW_Address" row[1] = prefix + " " + row_parts[1] + " " + row_parts[2] cursor.updateRow(row)
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.