1

I am attempting to add a new field called "TimeYrCode" to a list of shape files, and then add values to this field based on the value of the "gridcode" attribute, which represents different days of the year from 1-365.

The code to calculate this field works in ModelBuilder. Exported to a python script, the code reads as follows

# ADD AND CALCULATE FIELDS TO LAYERS IN THE WORKSPACE
for fc in fcList1:
    arcpy.AddField_management(fc, "TimeYrCode", "SHORT", "4", "", "", "", "NULLABLE", "NON_REQUIRED", "")
    arcpy.CalculateField_management(fc, "TimeYrCode", "myfunc( !gridcode!)", "PYTHON", "def myfunc(gridcode):\\n      if (gridcode >= 60) and (gridcode< 152):\\n          return 1\\n      elif (gridcode >= 152) and (gridcode< 244):\\n          return 2\\n      elif (gridcode >= 244) and (gridcode< 335):\\n          return 3\\n      elif (gridcode >= 335) or (gridcode< 60):\\n          return 4\\n      else:\\n          return 9")

When I attempt to run the code in PyCharm, I obtain the following error message

def myfunc(gridcode):\n      if (gridcode >= 60) and (gridcode< 152):\n          return 1\n      elif (gridcode >= 152) and (gridcode< 244):\n          return 2\n      elif (gridcode >= 244) and (gridcode< 335):\n          return 3\n      elif (gridcode >= 335) or (gridcode< 60):\n          return 4\n      else:\n          return 9
                                                                                                                                                                                                                                                                                                                                                ^
SyntaxError: unexpected character after line continuation character

Does anyone know what could be happening here? New to python and troubleshooting these kinds of issues

1

Something funky has happened to your newline characters (ie, \n). The \ has been escaped with another \, so instead of a newline, you're getting \\n.

Replace every instance of \\n with a \n newline (ie, delete one of the \ from each.

It is using the newline character escape sequence \n rather than actual newlines, because it is a separate 'script-within-a-script' in the 'BLOCK' parameter of the field calculator function call.

(Getting the indenting correct there would be difficult if you had to do it by hand, but since it was exported, it should all be correctly indented anyhow. I usually define the BLOCK separately into a variable, and include the variable in the function call.)

| improve this answer | |
  • I did not realize that \n was the standard format for line breaks. When I was exporting the script from ModelBuilder, line breaks were written as \\n, and therefore I accepted this as the format. By changing \\n to \n I was able to get the script going. I did not have to completely remove \n and replace with indents – Eli Simonson Feb 18 at 15:47
  • I've updated my answer to cater for the information in your comment, which is due to that part of the Python code actually being a separate script-within-a-script. Ie, a script being passed as a parameter within a function call. If this has resolved the problem, please mark the answer as correct so that people know the question isn't still looking for an answer. – Son of a Beach Feb 18 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.