2

I am attempting to create an update cursor to concatenate fields of a parcel ID based on the amount of characters in the MAPNO field. This is important because the spacing between MAPNO and LOT of the parcel ID is dependent on the number of characters in MAPNO. Below is my current syntax. The delete and append work just fine.

I am having an issue with the if len(row[1]) == '2' and the proper syntax to concatenate the Parcel_ID field.

import arcpy


def main():

inputParcels = arcpy.GetParameterAsText(0)
targetParcels = arcpy.GetParameterAsText(1)
parcelFields = ["Prefix","MAPNO","LOT","SUBLOT","Parcel_ID"]

try:
    arcpy.DeleteRows_management(targetParcels)
    arcpy.Append_management(inputParcels,targetParcels,"NO_TEST")

    with arcpy.da.UpdateCursor(targetParcels, parcelFields) as cursor:
        for row in cursor:
            if len(row[1]) == '2':
                row[4] = ((row[0][-2:]) + " " + row[1] + "    " + row[2] + "" + row[3])
            if len(row[1]) == '3':
                row[4] = ((row[0][-2:]) + " " + row[1] + "   " + row[2] + "" + row[3])
            if len(row[1]) == '4':
                row[4] = ((row[0][-2:]) + " " + row[1] + "  " + row[2] + "" + row[3])
            if len(row[1]) == '5':
                row[4] = ((row[0][-2:]) + " " + row[1] + " " + row[2] + "" + row[3])
            cursor.updateRow(row)

    del cursor


except Exception, e:
    import traceback, sys, os
    tb = sys.exc_info()[2]        
    arcpy.AddError("%s: Line %i"%("filename",tb.tb_lineno))
    arcpy.AddError(e)
finally:
    arcpy.AddMessage("Script is complete")
if __name__ == "__main__":
    main()
4

You need to remove the single quotes around your length comparison values, like this:

if len(row[1]) == 2:

The reason is that len() returns an integer value, and if your comparison value is surrounded by quotes, then that makes it a string. So even if returned value is 2, when you compare 2 == '2' python will return False.

3

As @alexGIS mentioned, your expression will always fail because you're testing an integer against a string for equality. I also wanted to point out you can greatly simplify your code by eliminating the if statements and string formatting do the work for you:

import arcpy

def main():

    inputParcels = arcpy.GetParameterAsText(0)
    targetParcels = arcpy.GetParameterAsText(1)
    parcelFields = ["Prefix","MAPNO","LOT","SUBLOT","Parcel_ID"]

    try:
        arcpy.DeleteRows_management(targetParcels)
        arcpy.Append_management(inputParcels,targetParcels,"NO_TEST")

        with arcpy.da.UpdateCursor(targetParcels, parcelFields) as cursor:
            for row in cursor:
                row[4] = ' '.join([row[0][-2:], '{:6}'.format(row[1])]) + row[2] + row[3]
                cursor.updateRow(row)

        del cursor


    except Exception, e:
        import traceback, sys, os
        tb = sys.exc_info()[2]        
        arcpy.AddError("%s: Line %i"%("filename",tb.tb_lineno))
        arcpy.AddError(e)
    finally:
        arcpy.AddMessage("Script is complete")
if __name__ == "__main__":
    main()

The magic happens in this line:

row[4] = ' '.join([row[0][-2:], '{:6}'.format(row[1])]) + row[2] + row[3]

From what I can tell, you are just joining the last 2 characters of your Prefix field, adding a space, then adding a variable amount of white space in between your MAPNO field and LOT (where the white space is 6 - the length of MAPNO), and finally concatenating the SUBLOT value. Is this correct?

If so, using the string formatting '{:6}' syntax will add the proper amount of space in between the MAPNO AND LOT if you want the total length of the MAPNO value and whitespace to be 6.

Take the following example (I have no idea what your values are supposed to look like):

>>> row = ['AABDE', '23', '41', 'A5']
>>> ' '.join([row[0][-2:], '{:6}'.format(row[1])]) + row[2] + row[3]
'DE 23    41A5'
  • I actually made a mistake in my first attempt. Try it with the revised line. – crmackey May 5 '16 at 1:48
  • 1
    Thanks! I went this route as it simplified the code and did exactly what I needed. – JarrettG May 5 '16 at 15:39

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.