I have a point layer with 900K Tree Point records. In that layer I have field (d_Species) that contains the tree species of each record. I've created a field (SpecieValu) to assign a value (0-3) for each record based upon the species of the tree point. There are hundreds of unique species and so I need to assign values based on a substring in the species field like "oak" or "cedar".
How do I use a wildcard search to find a substring and return a value?
My new field (SpecieValu) is a Double data type and the field I'm trying to reclass (d_Species) is a String data type. This is what I have so far but doesn't return anything but else 0:
This is what I have so far but doesn't return anything but else 0:
1 Answer 1
It looks like you're on the right track, it seems like you're just getting some Python and SQL syntax confused. If you are writing a SQL Expression (such as in a select by attribute or definition query), you would use a wildcard character such as the * in combination with the LIKE (which character depends on your data source and single or multi character).
However, in Python, you do not need to do so. When testing for a string in another string in python, it tests for the presence of the provided string, exactly as provided, anywhere in the other string. So in your case it was looking for *oak*
in the string, and Quercus rubra - northern red oak
does not contain *oak*
, so it didn't return 3. Instead, just get rid of the * and it would find oak
in Quercus rubra - northern red oak
.
Additionally, your screen shot looks like capitalization should not be an issue as the word your searching for should just be lowercase, but, it should be noted that if you do if 'oak' in 'Quercus Rubra - Northern Red Oak':
(note the capitalization), it would not find that if statement to be true and so wouldn't return 3. Therefore, to be safe, you may wish to re-write it like if 'oak' in d_Species.lower():
to avoid missing anything where the capitalization is mixed or upper case.
-
Thanks so much John this worked! Also I appreciate your detailed answer distinguishing between SQL/Python and lowercase etc. Can i put multiple substring values into a line? such as if "Quercus" or "Acer" in d_Species: return 3 ?Kurt Cederholm– Kurt Cederholm2019年03月21日 19:38:23 +00:00Commented Mar 21, 2019 at 19:38
-
You can put multiple tests in one If statement (separated by either or / and), but you would have to write the full comparison statement for each. Example:
if "quercus" in d_Species.lower() or "acer" in d_species.lower(): return 3
John– John2019年03月22日 14:16:55 +00:00Commented Mar 22, 2019 at 14:16
Explore related questions
See similar questions with these tags.
'oak' in 'pin oak'
and getTrue
as response.'*oak*' in 'pin oak'
will returnFalse
'