2

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!??

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jan 31, 2017 at 20:38
4
  • This is python function and it is different to SQL. E.g. first if should be TYPEFIELD[:2].lower()=="st" or similar Commented 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. Commented Jan 31, 2017 at 20:54
  • 1
    This is why I am suggesting using .lower() in my 1st comment Commented 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. Commented Jan 31, 2017 at 21:50

1 Answer 1

4

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
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered Jan 31, 2017 at 22:01
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.