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?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Mar 12, 2021 at 7:14

2 Answers 2

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)
answered Mar 12, 2021 at 7:46
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.

answered Mar 12, 2021 at 7:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.