# arcpy.CalculateField_management ERROR:

313
4
06-19-2018 01:49 PM
by Anonymous User
Not applicable

I'm trying to display the value of 1 or 2 fields based on the input of another.

arcpy.CalculateField_management(inPoints, fieldName10, "my_func(!RdType!, !Address!, !BENQUA! )", "PYTHON_9.3", "def my_func(val, fld1, fld2):\\n    if val in ('Pr'):\\n        return fld1 & " " & fld2\\n    else:\\n        return fld1")

1 Solution

Accepted Solutions
MVP Legendary Contributor

Anthony... did you try the field calculator expression to make sure it works?  And if it does, you can triple quote to get the correct formatting and indentation.

For example (untried)

``````# ---- tried once
val = 'a'
fld1 = 'b'
fld2 = 'c'

def my_func(val, fld1, fld2):
if val in ('Pr'):
return fld1 + " " + fld2
else:
return fld1

my_func(val, fld1, fld2)
'b'

# ---- then recall the function from command line and triple quote
#
"""def my_func(val, fld1, fld2):
if val in ('Pr'):
return fld1 + " " + fld2
else:
return fld1
"""
'def my_func(val, fld1, fld2):\n    if val in (\'Pr\'):\n        return fld1 + " " + fld2\n    else:\n        return fld1\n'

# ---- or

"""
def my_func(val, fld1, fld2):
if val in ('Pr'):
return fld1 + " " + fld2
else:
return fld1
"""
'\ndef my_func(val, fld1, fld2):\n    if val in (\'Pr\'):\n        return fld1 + " " + fld2\n    else:\n        return fld1\n'‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``````
4 Replies
MVP Esteemed Contributor

Hard to read this thing without the script provided as a .. script but this is not a valid Python expression:

``````return fld1 & " " & fld2
‍‍‍``````

This should be

``return fld1 + " " + fld2‍‍‍``
by Anonymous User
Not applicable

import arcpy

arcpy.env.overwriteOutput = True

#Variables
featureType = "Point"
fieldName1 = "N_S"
fieldName2 = "E_W"
fieldName3 = "RWDir"
fieldName4 = "STRT_NAM"
fieldName5 = "User_Defined_Digits"
fieldName6 = "RdType"
fieldName7 = "Temp"

expression1 = "int(((Abs([POINT_X] - 1894420.5138)) * 0.000189394)*100)"
expression2 = "int(((Abs([POINT_Y] - 322586.0736)) * 0.000189394)*100)"
expression3 = arcpy.GetParameterAsText(0)
expression4 = arcpy.GetParameterAsText(1)
expression5 = arcpy.GetParameterAsText(2)
expression6 = arcpy.GetParameterAsText(3)
expression7 = arcpy.GetParameterAsText(4)
if expression7 == '#' or not expression7:
expression7 = 'None'
expression8 = '[Temp] & [N_S] & [User_Defined_Digits] & " " & [RWDir] & " " & [STRT_NAM] & " " & [RdType]'
expression9 = '[Temp] & [E_W] & [User_Defined_Digits] & " " & [RWDir] & " " & [STRT_NAM] & " " & [RdType]'

arcpy.CalculateField_management(inPoints, fieldName1, expression1)
arcpy.CalculateField_management(inPoints, fieldName2, expression2)
arcpy.CalculateField_management(inPoints, fieldName3, expression3)
arcpy.CalculateField_management(inPoints, fieldName4, expression4)
arcpy.CalculateField_management(inPoints, fieldName5, expression5)
arcpy.CalculateField_management(inPoints, fieldName6, expression6)
arcpy.CalculateField_management(inPoints, fieldName7, expression7)
arcpy.CalculateField_management(inPoints, fieldName8, expression8)
arcpy.CalculateField_management(inPoints, fieldName9, expression9)
arcpy.CalculateField_management(inPoints, fieldName10, "my_func(!RWDir!, !NS_Address!, !EW_Address! )", "PYTHON_9.3", "def my_func(val, fld1, fld2):\\n \"\"\" NS_Address if the Prefix attribute is N or S\\n EW_Address if the RWDir attribute is E or W\\n \"\"\"\\n if val in ('N', 'S'):\\n return fld1\\n elif val in ('E', 'W'):\\n return fld2\\n else:\\n return None\\n")

#----------------------------------------------------------------------

#pulls quadrant attribute from BENQUA layer

rows = arcpy.SearchCursor("BENQUA")
for row in rows:

arcpy.SelectLayerByAttribute_management("BENQUA", "NEW_SELECTION", "\"OBJECTID\" = " + str(row.getValue("OBJECTID")))

#----------------------------------------------------------------------

#pulls city attribute from PostalArea layer

rows = arcpy.SearchCursor("PostalArea")
for row in rows:

arcpy.SelectLayerByAttribute_management("PostalArea", "NEW_SELECTION", "\"OBJECTID\" = " + str(row.getValue("OBJECTID")))

#----------------------------------------------------------------------

arcpy.CalculateField_management(inPoints, fieldName11, "my_func(!RdType!, !Address!, !BENQUA! )", "PYTHON_9.3", "def my_func(val, fld1, fld2):\\n if val in ('Pr'):\\n return fld1 + " " + fld2\\n else:\\n return fld1")

MVP Legendary Contributor

Anthony... did you try the field calculator expression to make sure it works?  And if it does, you can triple quote to get the correct formatting and indentation.

For example (untried)

``````# ---- tried once
val = 'a'
fld1 = 'b'
fld2 = 'c'

def my_func(val, fld1, fld2):
if val in ('Pr'):
return fld1 + " " + fld2
else:
return fld1

my_func(val, fld1, fld2)
'b'

# ---- then recall the function from command line and triple quote
#
"""def my_func(val, fld1, fld2):
if val in ('Pr'):
return fld1 + " " + fld2
else:
return fld1
"""
'def my_func(val, fld1, fld2):\n    if val in (\'Pr\'):\n        return fld1 + " " + fld2\n    else:\n        return fld1\n'

# ---- or

"""
def my_func(val, fld1, fld2):
if val in ('Pr'):
return fld1 + " " + fld2
else:
return fld1
"""
'\ndef my_func(val, fld1, fld2):\n    if val in (\'Pr\'):\n        return fld1 + " " + fld2\n    else:\n        return fld1\n'‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``````
by Anonymous User
Not applicable

Thanks for your help on this issue.