1

I'm trying to make a four character output based on concatenating a single character field (in1) with either 1 or 2 zeros, depending on the length of in2, and then concatenating that . I'm getting an indention error on this but I can't see where. I looked at the other posts with this issue and none of their solutions are working for me.

def fixRouteName( in1, in2, out ):
  if len(in1) == 2:
    out = in1 + "0" + in2
  elif len(in2)  < 2:
    out =  in1 + "00" + in2
  else:
    out = in1 + in2
  return out 

__esri_field_calculator_splitter__

fixRouteName( !Exist_Partition! , !Exist_Route! , !Route! )

Here is the error message:

Description The code block used by the Calculate Field or Calculate Value tool has a syntax error. This error message provided will list the specific Python syntax error.

Solution This error code covers a number of Python syntax errors, for example, Parsing error: unindent does not match any outer indentation level (line 3).

The above is a Python syntax error. Line 3 has invalid indentation. For specific Python issues, consult the Python documentation for more information, or consult the Calculate Field, Calculate Value, or Analyze Tools For Pro help for more information on these tools.

When returned by a script tool or when evaluating a script tool using Analyze Tools For Pro, this error can also indicate that the tool's source script cannot be found.

6
  • What are the field types of the three fields that you are passing values from into your expression?
    – PolyGeo
    Commented Aug 3, 2018 at 22:44
  • @PolyGeo they are strings, text
    – geoJshaun
    Commented Aug 3, 2018 at 22:50
  • Do either of in1 or in2 have null values? If so, concatenating a NoneType and a string might give an error.
    – PolyGeo
    Commented Aug 3, 2018 at 23:48
  • @PolyGeo no nulls
    – geoJshaun
    Commented Aug 3, 2018 at 23:51
  • What version of ArcGIS Desktop are you using?
    – PolyGeo
    Commented Aug 4, 2018 at 1:24

1 Answer 1

2

I tried your code on my machine (running ArcMap 10.3.1) and it worked, so I could not replicate your error. Try using an UpdateCursor in the ArcMap python interpreter (substitute 'your_shapefile' for the name of your shapefile):

import arcpy
with arcpy.da.UpdateCursor('your_shapefile', ['Exist_Partition', 'Exist_Route', 'Route']) as cursor:
    for row in cursor:
        if len(row[0]) == 2:
            row[2] = row[0] + "0" + row[1]
        elif len(row[1]) < 2:
            row[2] = row[0] + "00" + row[1]
        else:
            row[2] = row[1] + row[2]
        cursor.updateRow(row)

Also, ensure both the input fields ('Exist_Partition' and 'Exist Route') are text.

On a final note, in your original function you passed out as an input, but you don't need to. The function need only accept two arguments. This is the function that worked in field calculator on my machine:

def fixRouteName( in1, in2 ):
  if len(in1) == 2:
    out = in1 + "0" + in2
  elif len(in2) < 2:
    out = in1 + "00" + in2
  else:
    out = in1 + in2
  return out
1
  • Thanks Chris. That makes sense and it works. I went for the second solution.
    – geoJshaun
    Commented Aug 3, 2018 at 23:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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