简体   繁体   中英

“Unexpected character after line continuation character” error

I have read a few answers about the same error message, but don't understand how they apply to my code. I have no clue what is wrong with it. I'm exporting a model from ArcGIS to Python. My model is validated in ArcMap , but when I export the code I get the message.

Local variables:

PopulationEllipse_Buffered = "C:\\GIS_Cert\\GIS211\\FinalProject\\FinalLongmont.gdb\\PopulationEllipse_Buffered"
PopulationEllipse_Buffered_w_New_Field = PopulationEllipse_Buffered
PopulationEllipse_Buffered_w_Ranks = PopulationEllipse_Buffered_w_New_Field

Process: Add Field (3)

arcpy.AddField_management(PopulationEllipse_Buffered, "PopRank", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

Process: Calculate Field (3)

arcpy.CalculateField_management(PopulationEllipse_Buffered_w_New_Field, "PopRank", "calculateIF( !ToBufDist! )", "PYTHON", "def calculateIF(dist):\n if(dist == 0):\n return 5\n elif(dist == 0.5):\n return 4\n else:\n return 3")

print(arcpy.GetMessages(2))

Well, your function string is not valid Python because the clauses of the if statement are not indented:

def calculateIF(dist):
 if(dist == 0):
 return 5
 elif(dist == 0.5):
 return 4
 else:
 return 3

Those return statements need to be indented further than the if / elif / else are (eg add another space before the return s and after the \n s). But the error message you're getting is not the one I'd expect if that were the problem you're running into.

So my professor doesn't know why this error happens with the codeblock, but the solution is to remove it and do several select by attributes and after each calculate the field with the selections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM