2

I have a Python expression that I use in Field Calculator.

!PROPR_NAME!.split(",")[0]

I am writing a script in PyCharm and I am wanting to add this expression, however I am uncertain of how to re-write it in ArcPy code.

Does anyone know the code for split?

3

If you want to use the da.UpdateCursor, you can try:

import arcpy

feature_class = r'C:\folder\data.gdb\features123'
field_to_split = 'PROPR_NAME'
field_to_calculate = 'PROPR_NAME_split'

with arcpy.da.UpdateCursor(feature_class, [field_to_split, field_to_calculate]) as cursor:
    for row in cursor:
        row[1] = row[0].split(',')[0]
        cursor.updateRow(row)
0
0

The split method you are using is from Python, and in the Field Calculator you are applying that method to the string returned from each row in the PROPR_NAME field.

I think the easiest way to get the same done using ArcPy is to open the Calculate Field tool, configure it the same as you did for the Field Calculator, run it, and then use the Results window to Copy As Python Snippet.

You can paste that Python Snippet, which will have the correct syntax, into your script.

Alternatively, you could investigate using arcpy.da.UpdateCursor() instead of the Calculate Field tool.

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.