2

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)

And here are the fields I'm pulling from:

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Mar 10, 2022 at 3:30
1
  • 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. Commented Mar 10, 2022 at 3:37

1 Answer 1

3

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

answered Mar 10, 2022 at 6:33
1
  • 1
    Thank you so much, this has been astoundingly helpful Commented Mar 10, 2022 at 16:18

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.