Locating a substring in Field Calculator

3340
3
11-29-2019 04:14 PM
SimonWebster
Occasional Contributor

I have what should be a fairly straightforward operation failing in ArcGIS Pro 2.4, and cannot for the life of me work out why. 

If the field "assettype" contains a portion of the search string, then set the value of assettype_groupup to the value I return. 

Eg, if "assetttype" contains the string "Building |Residential |Large ", and I test whether it contains the term "Residential", and that evaluates to true, then return the string "Residential". 

Currently the code does not seem to be returning any result / has no effect, and appears to run too quickly (2-3 seconds for 3,000,000 lines).

If I try a ternary statement, which means using a single term at a time, it seems to work just fine. 

Can you see any obvious issues with the setup below

#Expression
func(!assettype!)

# Code block
def func(input😞
    if 'Residential' in input:
        return 'Residential'
    elif 'Industrial/Utilities' in input:
        return 'Industrial/Utilities'
    elif 'Transport/Infrastructure' in input:
        return 'Transport/Infrastructure'
    elif 'Conservation/National Park' in input:
        return 'Conservation/National Park'
    elif 'Recreational/Open Space' in input:
        return 'Recreational/Open Space'
    elif 'Mixed Use' in input:
        return 'Mixed Use'
    elif 'Community Use' in input:
        return 'Community Use'
    elif 'Rural/Primary Production' in input:
        return 'Rural/Primary Production'
    elif 'Special Use' in input:
        return 'Special Use'
    elif 'Unknown' in input:
        return 'Unknown'
    else:
        ''

Looks like:

0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus

What is in the tables?  (a couple of rows as an example)

If you are using the 'in' operator, then it would suggest that "Residential" may only be part of a potential value, otherwise you could just use the equality check.

I am sure you have ruled out text case as a culprit. And that the output field is indeed text.

SimonWebster
Occasional Contributor

As it turns out, my issue was mostly likely related to using a reserved word. 

Answer here: python - Locating a substring in Field Calculator using ArcGIS Pro - Stack Overflow 

walshjupes
New Contributor

Python has no substring methods like substring() or substr(). Instead, you can use slice syntax to get parts of existing strings. Python slicing is a computationally fast way to methodically access parts of your data. The colons (:) in subscript notation make slice notation - which has the arguments, start, stop and step . It follows this template:

Parameters are enclosed in the square brackets.
Parameters are separated by colon.

string[start: end: step]

  • start - Starting index of string, Default is 0.
  • end - End index of string which is not inclusive .
  • step - An integer number specifying the step of the slicing. Default is 1.

 

0 Kudos