HOW TO
In ArcGIS Pro, the leading, middle, and ending text can be extracted from a string field using the Python parser in the Calculate Field tool. This is useful for data management, better readability, and identifying unique identifiers. For instance, when dealing with datasets containing detailed location descriptions in text fields, extracting different parts of the text can simplify spatial querying and classification tasks.
This article provides the workflow to extract the leading, middle, and ending text from a string field using the Python parser in the Calculate Field tool to populate them in another field. In this example, the ending text is extracted from a field named Location_Description.
extract_characters(!Field_Name!)
def extract_characters(sentence): space_index = sentence.find(' ') first_word = sentence[:space_index] if space_index != -1 else sentence num_characters = len(first_word) extracted_characters = first_word[:num_characters] return extracted_characters
def extract_characters(sentence): space_index = sentence.find(' ') last_space_index = sentence.rfind(' ') middle_word = sentence[space_index + 1:last_space_index] if space_index != -1 and space_index != last_space_index else sentence num_characters = len(middle_word) extracted_characters = middle_word[:num_characters] return extracted_characters
def extract_characters(sentence): space_index = sentence.rfind(' ') last_word = sentence[space_index + 1:] if space_index != -1 else sentence num_characters = len(last_word) extracted_characters = last_word[:num_characters] return extracted_characters
The image below displays the Ext_Description field in the attribute table containing the ending text extracted from the Location_Description field.
Get help from ArcGIS experts
Download the Esri Support App