HOW TO

Separate numbers and letters in a field for ArcGIS Pro

Last Published: September 27, 2023

Summary

In ArcGIS Pro, Python expressions can be used to separate numeric and text fields from a specified text data type field with alphanumeric values. This article provides an example and instructions to create a new numeric and text field by referencing the text field with alphanumeric values using Python expressions.

The attribute table below shows the alphanumeric Name field.

The attribute table in ArcGIS Pro

Procedure

  1. In the Contents pane, right-click the feature class and click Attribute Table to open the attribute table of the feature class.
  2. Click Calculate to open the Calculate Field tool.
Note:
Refer to ArcGIS Pro: Calculate Field (Data Management) for more information on the parameters in the Calculate Field tool.
  1. In the Calculate Field dialog box, configure the parameters.
    1. For Input Table, select the feature class. In this example, TestGrid is selected.
    2. For Field Name (Existing or New), select the field to be edited or create a new field. In this example, the NumberTestLong field is created.
    3. For Field Type, select Long (32-bit integer).
    4. For Expression Type, select Python 3.
    5. Specify the following expression in the Expression parameter. In this example, <textField> is named Name.
conv(<textField>)
    1. Specify the following expression in the Code Block parameter.
def conv(<variableName>):
    <variableName1> = ''
    for chr in <variableName>:
        if (ord(chr) >= 48 and ord(chr) <= 57) or ord(chr) == 47 or ord(chr) == 32 or ord(chr) == 45:
            <variableName1> = <variableName1> + chr
    return <variableName1>
The following code block is an example of the full working script.
def conv(nameField):
    extractNum = ''
    for chr in nameField:
        if (ord(chr) >= 48 and ord(chr) <= 57) or ord(chr) == 47 or ord(chr) == 32 or ord(chr) == 45:
            extractNum = extractNum + chr
    return extractNum
    1. Click Verify to run a test of the expression. If the expression is valid, click OK.
The Calculate Field tool pane with the parameters to be filled
Note:
To display leading zeroes for the numeric field, refer to How To: Add leading zeroes to numeric fields in ArcGIS Pro for instructions.
  1. In the Attribute Table, click Calculate to open the Calculate Field tool.
  2. In the Calculate Field dialog box, configure the parameters.
    1. For Input Table, select the feature class. In this example, TestGrid is selected.
    2. For Field Name (Existing or New), select the field to be edited or create a new field. In this example, the AlphabetTest field is created.
    3. For Field Type, select Text.
    4. For Expression Type, select Python 3.
    5. Specify the following expression in the Expression parameter. In this example, <textField> is named Name.
conv(<textField>)
    1. Specify the following expression in the Code Block parameter.
def conv(<variableName>):
    <variableName1> = ''
    for chr in <variableName>:
        if not((ord(chr) >= 48 and ord(chr) <= 57) or ord(chr) == 47 or ord(chr) == 32 or ord(chr) == 45):
            <variableName1> = <variableName1> + chr
    return <variableName1>
The following code block is an example of the full working script.
def conv(nameField):
    extractStr = ''
    for chr in nameField:
        if not((ord(chr) >= 48 and ord(chr) <= 57) or ord(chr) == 47 or ord(chr) == 32 or ord(chr) == 45):
            extractStr = extractStr + chr
    return extractStr
    1. Click Verify to run a test of the expression. If the expression is valid, click OK.
The Calculate Field tool pane with the parameters to be filled

The image below shows the new numeric and text fields added to the attribute table.

The attribute table with the new numeric and text field added

Article ID: 000031133

Software:
  • ArcGIS Pro 3 1
  • ArcGIS Pro 3 0
  • ArcGIS Pro 2 9x

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options