I'm fairly new to Python and I'm trying to amalgamate the zip codes from the fields of !OWADR2!, !OWADR3!, and !OWADR4! into a new field I've created. Basically I want to make a block of code in the new field's "calculate field" that checks if the last 5 characters of each address field is a string of numbers and pulls it if it is, but if it isn't it will iterate to the next address and so on.
With my limited knowledge of Python this is what I came up with:
In the expression field:
zipcodepull(!OWADR2![-5:], !OWADR3![-5:], !OWADR4![-5:])
and in the code block:
def zipcodepull(zip1, zip2, zip3):
if zip1 == int:
return zip1
elif zip2 == int:
return zip2
elif zip3 == int:
return zip3
else:
return int(0)
-
As a starting point, I think you will need to use type() method to check the data type of a variable. zip1.type() = 'int' or similar.nr_aus– nr_aus2022年03月10日 03:37:29 +00:00Commented Mar 10, 2022 at 3:37
1 Answer 1
if you replace
zipcodepull(!OWADR2![-5:], !OWADR3![-5:], !OWADR4![-5:])
with
zipcodepull(!OWADR2!, !OWADR3!, !OWADR4!)
And each if elif with for example:
if zip1[-5:].isnumeric():
return int(zip1[-5:])
Your code should work
-
1Thank you so much, this has been astoundingly helpfulmrmcbreakfast– mrmcbreakfast2022年03月10日 16:18:08 +00:00Commented Mar 10, 2022 at 16:18
Explore related questions
See similar questions with these tags.