CalculateFieldManagement failing after upgrading to arcgis 10.8.1

2081
9
Jump to solution
03-04-2021 01:03 PM
by Anonymous User
Not applicable
I'm getting an error in Python when executing script:
 
Python syntax error: Parsing error SyntaxError: unexpected character after line continuation character (line 1)
 
arcpy.CalculateField_management(Applicants, "UGA_Flag""UGAFlag(!UGA_Flag!, !PER_Area!)""PYTHON_9.3""def UGAFlag(UGA_Flag, PER_Area):\\n  if PER_Area < 0.05:\\n    return \"Outside (<5%) of Urban Growth Area\"\\n  elif PER_Area > 0.95:\\n    return \"Mostly (>95%) within Urban Growth Area\"\\n  else:\\n    return \"Partially within Urban Growth Area\"")
 
I've searched for explanations and I understand that it likely has to do with backslash vs forwardslash. I ran this script in January without an issue (I was using 10.5.1 desktop with same version of python).
 
Any helpful explanation is greatly appreciated.
0 Kudos
4 Solutions

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

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)

... sort of retired...

View solution in original post

DanPatterson
MVP Esteemed Contributor
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


... sort of retired...

View solution in original post

0 Kudos
DanPatterson
MVP Esteemed Contributor

what happened to the indentation that was in my example?


... sort of retired...

View solution in original post

0 Kudos
by Anonymous User
Not applicable

 

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

 

 

 

View solution in original post

0 Kudos
9 Replies
DanPatterson
MVP Esteemed Contributor

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)

... sort of retired...
by Anonymous User
Not applicable

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.

0 Kudos
DanPatterson
MVP Esteemed Contributor
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


... sort of retired...
0 Kudos
by Anonymous User
Not applicable

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!

0 Kudos
DanPatterson
MVP Esteemed Contributor

what happened to the indentation that was in my example?


... sort of retired...
0 Kudos
by Anonymous User
Not applicable

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
"""
0 Kudos
by Anonymous User
Not applicable

I just added the comments as I'm starting to hack at it to try and figure out why I'm getting the error shown below:

 

0 Kudos
by Anonymous User
Not applicable

 

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

 

 

 

0 Kudos
by Anonymous User
Not applicable

Thanks again for your help.. I see the errors and have fixed it and I'm in good shape. Thanks again!!!

0 Kudos