arcpy.CalculateField_management ERROR:

400
4
Jump to solution
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")

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

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'

View solution in original post

4 Replies
curtvprice
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‍‍
0 Kudos
by Anonymous User
Not applicable

import arcpy

arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"G:\GIS\Anthony\GDB\Addressing_tool\Addressing_tool_MOD\Data\gdb\Addressing.gdb" 
arcpy.MakeFeatureLayer_management ("Address", "Address")
arcpy.SelectLayerByAttribute_management ("Address", "NEW_SELECTION", '"NS_Address" is Null')

#Variables
inPoints = r"Address" 
featureType = "Point"
fieldName1 = "N_S"
fieldName2 = "E_W"
fieldName3 = "RWDir"
fieldName4 = "STRT_NAM"
fieldName5 = "User_Defined_Digits"
fieldName6 = "RdType"
fieldName7 = "Temp"
fieldName8 = "NS_Address"
fieldName9 = "EW_Address"
fieldName10 = "Address"
fieldName11 = "Address"

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.AddXY_management(inPoints)

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


arcpy.MakeFeatureLayer_management("G:\GIS\Anthony\GDB\Addressing_tool\Addressing_tool_MOD\Data\gdb\Addressing.gdb\Address", "Address_lyr")
arcpy.MakeFeatureLayer_management("G:\GIS\Anthony\GDB\Addressing_tool\Addressing_tool_MOD\Data\gdb\Addressing.gdb\BENQUA", "BENQUA_lyr")

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

arcpy.SelectLayerByAttribute_management("BENQUA", "NEW_SELECTION", "\"OBJECTID\" = " + str(row.getValue("OBJECTID")))
arcpy.SelectLayerByLocation_management("Address", "INTERSECT", "BENQUA", "", "NEW_SELECTION")
arcpy.CalculateField_management("Address", "BENQUA", "'{0}'".format(str(row.getValue("BENQUA"))), "PYTHON_9.3", "")

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

#pulls city attribute from PostalArea layer

arcpy.MakeFeatureLayer_management("G:\GIS\Anthony\GDB\Addressing_tool\Addressing_tool_MOD\Data\gdb\Addressing.gdb\Address", "Address_lyr")
arcpy.MakeFeatureLayer_management("G:\GIS\Anthony\GDB\Addressing_tool\Addressing_tool_MOD\Data\gdb\Addressing.gdb\PostalArea", "PostalArea")

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

arcpy.SelectLayerByAttribute_management("PostalArea", "NEW_SELECTION", "\"OBJECTID\" = " + str(row.getValue("OBJECTID")))
arcpy.SelectLayerByLocation_management("Address", "INTERSECT", "PostalArea", "", "NEW_SELECTION")
arcpy.CalculateField_management("Address", "PostalArea", "'{0}'".format(str(row.getValue("PostalArea"))), "PYTHON_9.3", "")


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

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")

0 Kudos
DanPatterson_Retired
MVP Emeritus

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.

0 Kudos