I'm trying to do a Field Calculation in ArcGIS 10.3 (Python 2.7) that uses the Codeblock (Pre-Logic Script Code) to evaluate the attributes in a different field to inform the calculation).
Here's an example of what I'm trying to do (but getting syntax errors):
def evalStreetType(TYPEFIELD):
if (TYPEFIELD LIKE "St*"):
return "Street"
elif (TYPFIELD LIKE "Av*"):
return "Avenue"
elif (TYPEFIELD LIKE "P*k*wy*):
return "Parkway"
else:
return TYPEFIELD
And then the field calculator will say [StreetType] =
evalStreetType(!AbrvStTyp!)
Hopefully I didn't introduce new syntax errors as I simplify things to post in this question. I believe the crux of my problem is that I'm phrasing my "LIKE" statement and using wildards in a SQL sort of way, and it's not how Python does it. But I'm not sure now to evaluate strings with wildcards in python (....obviously).
Can someone help me out!??
-
This is python function and it is different to SQL. E.g. first if should be TYPEFIELD[:2].lower()=="st" or similarFelixIP– FelixIP2017年01月31日 20:51:05 +00:00Commented Jan 31, 2017 at 20:51
-
Thanks Felix - I understand what you're saying, but I'm looking for a way to use wildcards, so that I don't have to anticipate every possible way that each street type could be abbreviated (i.e. "St", "St.", "Str.", "Av", "Av.", "Ave", "Pkwy", "Pky") etc.Matt Goodman– Matt Goodman2017年01月31日 20:54:43 +00:00Commented Jan 31, 2017 at 20:54
-
1This is why I am suggesting using .lower() in my 1st commentFelixIP– FelixIP2017年01月31日 21:07:29 +00:00Commented Jan 31, 2017 at 21:07
-
That does help, somewhat Felix. As you can tell, I'm really just 'starting out' when it comes to python. My street type example in my post was a poor attempt to simplify/genericize my issue - it's not the actual data I'm working with. The actual data requires that I search for substrings within (in the middle of) the larger string. I found that the "in" operator works well for my purposes! I'll post an answer with what ended up with.Matt Goodman– Matt Goodman2017年01月31日 21:50:22 +00:00Commented Jan 31, 2017 at 21:50
1 Answer 1
I found my own resolution and it's extremely simple. I just didn't know that it existed. It uses the "in" operator.
def evalStreetType(TYPEFIELD):
if "ST" in TYPEFIELD.upper():
return "Street"
elif "AV" in TYPEFIELD.upper():
return "Avenue"
elif ("PK" in TYPEFIELD.upper()) and ("Y" in TYPEFIELD.upper()):
return "Parkway"
else:
return TYPEFIELD
Explore related questions
See similar questions with these tags.