2

I am currently working (in ArcMap 10.4) with two datasets that use different nomenclature for their assessor parcel numbers (APN) values. In one dataset the APN is a string dataset with the format "12345678". In the other dataset the APN is again a string but displayed as "123-456-78".

My goal is to add '-' characters to the correct position within the first string, to allow me to perform a join between the two APN fields respectively. Currently my script within the code block is seen below. I have created a new field titled "APN" and am hoping to populate this new field using the PRCL_ID string.

enter image description here

I am still not confident with scripting within the field calculator tool and have based this code on a similar problem in another thread. Does anyone have any suggestions on the best way to approach this problem?

1 Answer 1

2

I think you should use the da.UpdateCursor instead of Field Calculator. It will give you much more flexibility and is well worth learning. The code can be executed in the Python window of ArcGIS.

import arcpy

feature_class = r'C:\database.gdb\feature_class' #Change to match your data
fields = ['field_to_read','field_to_write'] #Change to match your data

with arcpy.da.UpdateCursor(feature_class, fields) as cursor:
    for row in cursor:
        row[1] = '-'.join([str(row[0])[i:i+3] for i in range(0, len(str(row[0])), 3)])
        cursor.updateRow(row)

Example from ArcGIS Pro but it is the same in ArcGIS Desktop:

enter image description here

1
  • 1
    Thanks, this definitely solved my issue! I don't know why I was stuck on doing this in the field calculator. Having to use a "def" function was throwing me off. Jan 16, 2018 at 17:12

Your Answer

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

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