Excerpt a String from a Series or List

This tutorial covers using the expression and code block options of the Field Calculator in ArcGIS to split a string (text) list of items and returning a specific element of the list.

 

Included in this tutorial

  • Using a python code block to split and excerpt text from a series or list in ArcGIS Pro.

  • Note: the python works outside ArcGIS as well.

Credits: L. Meisterlin (2021)

 

First, access the Calculate Field tool. Then, specify the table, field, and data type.

Use the expression and code block below to…

  • Split a string field (field = FieldName) at a certain character (separator = x, which is noted in double quotes), creating an indexed list of elements (defined as “list” in the code block).

  • Then, return the element in a specific location from the list. That location is defined as “indexlocation” in the code block and specified as an integer in the expression.

Expression

 
element(!FieldName!,"x",integer)

Code Block

 
def element(field,separator,indexlocation)
    list = field.split(separator)
    return list[indexlocation]

TIP: The index locations of the list are numbered starting at 0. (So, the first element of the list is in location 0.) To count backward, use negative numbers (i.e., the last element is in location -1).

Example 1

Using the code block above: If FieldA contains the value ABC_XYZ, then the expression element(!FieldA!,"_",0) returns ABC.

Example 2

Using the same code block above: If FieldB contains the value Red, Orange, Yellow, then the expression element(!FieldB!,", ",-1) returns Yellow.

 
Previous
Previous

Copying Values to a New Field

Next
Next

Concatenating Fields