Solved! Go to Solution.
Add an extra line to your script to separate the codeblock so you don't have to mess with spaces and your double quote errors
block = """
def UGAFlag(UGA_Flag, PER_Area):
if PER_Area < 0.05:
return 'Outside (<5%) of Urban Growth Area'
elif PER_Area > 0.95:
return 'Mostly (>95%) within Urban Growth Area'
else:
return 'Partially within Urban Growth Area'
"""
then something like this (totally not tested...
arcpy.CalculateField_management(Applicants, "UGA_Flag",
"UGAFlag(!UGA_Flag!, !PER_Area!)",
"PYTHON_9.3",
block)
block = """
def PerPasture(TOTAcres, Past_Acres, ID):
if ID == 212:
if TOTAcres not in (0, None):
return Past_Acres / TOTAcres
return -99999
else:
return 0
"""
unless your "block" was a formatting issue, perhaps TOTAcres was either None or 0
what happened to the indentation that was in my example?
block = """
def PerPasture(TOTAcres, Past_Acres, ID):
if ID == 212:
#if TOTAcres not in (0, None):
return Past_Acres! / !TOTAcres
#return -99999
else:
return 0
"""
Pasture_Farms_Dissolved = arcpy.CalculateField_management(Pasture_Farms_Dissolved, "PER_Past", block)
This is the particular code block for this calculation that executes without issue on a workstation running ArcGIS 10.5.1:
#Pasture_Farms_Dissolved = arcpy.CalculateField_management(Pasture_Farms_Dissolved, "PER_Past", "PerPasture(!TOTAcres!, !Past_Acres!, !ID!)", "PYTHON_9.3", "def PerPasture(TOTAcres, Past_Acres, ID):\\n if ID == 212:\\n return long(Past_Acres) / long(TOTAcres)\\n else:\\n return 0")
Add an extra line to your script to separate the codeblock so you don't have to mess with spaces and your double quote errors
block = """
def UGAFlag(UGA_Flag, PER_Area):
if PER_Area < 0.05:
return 'Outside (<5%) of Urban Growth Area'
elif PER_Area > 0.95:
return 'Mostly (>95%) within Urban Growth Area'
else:
return 'Partially within Urban Growth Area'
"""
then something like this (totally not tested...
arcpy.CalculateField_management(Applicants, "UGA_Flag",
"UGAFlag(!UGA_Flag!, !PER_Area!)",
"PYTHON_9.3",
block)
Thanks again for your help. I have been updating the script in other areas use similar logic on different fields. It's been working fairly well (and by that I mean the code is executing without error)...
On a particular field however I'm getting an error:
Traceback (most recent call last):
File "\\Nq-cluster1\share-appl\GISAppl\FarmRanking\Minimum Check\Minimum Criteria v1_1.py", line 442, in <module>
Pasture_Farms_Dissolved = arcpy.CalculateField_management(Pasture_Farms_Dissolved, "PER_Past", block)
File "C:\Program Files (x86)\ArcGIS\Desktop10.8\ArcPy\arcpy\management.py", line 3647, in CalculateField
raise e
ExecuteError: ERROR 999999: Error executing function.
Syntax error
Failed to execute (CalculateField).
block = """
def PerPasture(TOTAcres, Past_Acres, ID):
if ID == 212:
return Past_Acres / TOTAcres
else:
return 0
"""
Pasture_Farms_Dissolved = arcpy.CalculateField_management(Pasture_Farms_Dissolved, "PER_Past", block)
I did a search for the ERROR 999999: Error executing function but I haven't found anything helping to determine the issue.
How do I solve this kind of error? The block is simpler than other blocks that run without a problem. Help is greatly appreciated.
block = """
def PerPasture(TOTAcres, Past_Acres, ID):
if ID == 212:
if TOTAcres not in (0, None):
return Past_Acres / TOTAcres
return -99999
else:
return 0
"""
unless your "block" was a formatting issue, perhaps TOTAcres was either None or 0
Do you have another suggestion?
I'm not having much luck..
Here's the code that runs fine in Arc10.5
Pasture_Farms_Dissolved = arcpy.CalculateField_management(Pasture_Farms_Dissolved, "PER_Past", "PerPasture(!TOTAcres!, !Past_Acres!, !ID!)", "PYTHON_9.3", "def PerPasture(TOTAcres, Past_Acres, ID):\\n if ID == 212:\\n return long(Past_Acres) / long(TOTAcres)\\n else:\\n return 0")
And here's what's throwing the error:
block = """
def PerPasture(TOTAcres, Past_Acres, ID):
if ID == 212:
return Past_Acres / TOTAcres
else:
return 0
"""
Pasture_Farms_Dissolved = arcpy.CalculateField_management(Pasture_Farms_Dissolved, "PER_Past", block)
If you've got a suggestion on how I can solve it I'd appreciate it!
what happened to the indentation that was in my example?
Sorry about that.
block = """
def PerPasture(TOTAcres, Past_Acres, ID):
if ID == 212:
#if TOTAcres not in (0, None):
return Past_Acres! / !TOTAcres
#return -99999
else:
return 0
"""
block = """
def PerPasture(TOTAcres, Past_Acres, ID):
if ID == 212:
#if TOTAcres not in (0, None):
return Past_Acres! / !TOTAcres
#return -99999
else:
return 0
"""
Pasture_Farms_Dissolved = arcpy.CalculateField_management(Pasture_Farms_Dissolved, "PER_Past", block)
This is the particular code block for this calculation that executes without issue on a workstation running ArcGIS 10.5.1:
#Pasture_Farms_Dissolved = arcpy.CalculateField_management(Pasture_Farms_Dissolved, "PER_Past", "PerPasture(!TOTAcres!, !Past_Acres!, !ID!)", "PYTHON_9.3", "def PerPasture(TOTAcres, Past_Acres, ID):\\n if ID == 212:\\n return long(Past_Acres) / long(TOTAcres)\\n else:\\n return 0")
Thanks again for your help.. I see the errors and have fixed it and I'm in good shape. Thanks again!!!