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.
Solved! Go to Solution.
Make the address field active.
put the function in the code block.
the expression in the expression box
and calculate away
def my_func(val, fld1, fld2)
""" NS_Address if the Prefix attribute is N or S
EW_Address if the Prefix attribute is E or W
"""
if val in ('N', 'S'):
return fld1
elif val in ('E', 'W'):
return fld2
else:
return None
expression
my_func(!Prefix!, !NS_Address!, !EW_Address! )
Something like this? Untested code...
http://pro.arcgis.com/en/pro-app/arcpy/data-access/updatecursor-class.htm
https://www.tutorialspoint.com/python/string_split.htm
Python IF...ELIF...ELSE Statements
import arcpy feature_class_path =r"xxxx" 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)
Make the address field active.
put the function in the code block.
the expression in the expression box
and calculate away
def my_func(val, fld1, fld2)
""" NS_Address if the Prefix attribute is N or S
EW_Address if the Prefix attribute is E or W
"""
if val in ('N', 'S'):
return fld1
elif val in ('E', 'W'):
return fld2
else:
return None
expression
my_func(!Prefix!, !NS_Address!, !EW_Address! )
Dan,
Thanks for the reply. I received the error below when attempting to run that calculation.
I just had to add a colon after the first line. The expression ran perfectly! Thank you!!!
Anthony, no problem
You should close the question so others can find the answer
Assumed Answered....
I suppose