# Calculate Field with Python 3 / <Null> values

989
5
09-04-2019 01:01 PM
Highlighted
Occasional Contributor

I'm attempting to concatenate several fields, where one of the source fields is a double, the rest are string, and all of them have some <Null> values.  If I use the following syntax in Calculate Field in Pro 24.1/Python 3:

str(!AddrPrimaryHighNo!) + " " + str(!StreetPreDrctnAbbrev!) + " " + str(!StreetName!) + " " + str(!StreetSuffixAbbrev!) + " " + str(!StreetPostDrctnAbbrev!)

I get values like:

1498.0 None AVENUE K NONE NONE

where I would expect:

1498  AVENUE K

StreetPreDrctnAbbrev is Text

StreetName is Text

StreetSuffixAbbrev is Text

StreetostDrctnAbbrev is Text

If I perform the field calculation below in ArcMap 10.5.1/VB Script, it works out just fine:

[AddrPrimaryHighNo]&" "& [StreetPreDrctnAbbrev]&" "& [StreetName]&" "& [StreetSuffixAbbrev]&" "& [StreetPostDrctnAbbrev]

Meanwhile I'm just going to go back to ArcMap.  Thanks

-Justin

Tags (3)
1 Solution

Accepted Solutions
Highlighted
MVP Regular Contributor

,

You are correct, I should not assume Justin would understand how to place a def in a code block.  I was also using my phone to reply while traveling.  Below are the call, def and a Calculate Field screen shot to use a function called "joinAddress" with the needed address fields which returns the assembled address string less any None values.

You call the function using:

The Code Block:

``def joinAddress(a,b,c,d,e):    flds = [a,b,c,d,e]    return " ".join([str(i) for i in flds if i is not None])‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``

Side note: If all your fields were string you could also use " ".join(filter(None, flds)).  You can still use this by changing the following; flds = [str(a),b,c,d,e].  I prefer the method above as you can also change the str(i) to str(i).strip() to remove any leading or training spaces that may be in your fields.

Calculate Field:

If you do not want to use the code block with a call, you can use the following as a calculate statement:

" ".join([str(i) for i in [!AddrPrimaryHighNo!, !StreetPreDrctnAbbrev!, !StreetName!, !StreetSuffixAbbrev!, !streetPostDrctnAbbrev!] if i is not None])

5 Replies
Highlighted
MVP Regular Contributor

You can use something like the following as a def in a code block:

``flds = [!AddrPrimaryHighNo!, !StreetPreDrctnAbbrev!, !StreetName!, !StreetSuffixAbbrev!, !StreetPostDrctnAbbrev!]" ".join([str(i) for i in flds if i is not None])‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``

` `

Highlighted
MVP Esteemed Contributor

Although I agree with this approach, it won't work within the Field Calculator unless the user creates a code block.  Creating a function in a code block and passing the fields to it isn't difficult, I am just not sure whether that is more than the OP wants to deal with.

Highlighted
MVP Regular Contributor

,

You are correct, I should not assume Justin would understand how to place a def in a code block.  I was also using my phone to reply while traveling.  Below are the call, def and a Calculate Field screen shot to use a function called "joinAddress" with the needed address fields which returns the assembled address string less any None values.

You call the function using:

The Code Block:

``def joinAddress(a,b,c,d,e):    flds = [a,b,c,d,e]    return " ".join([str(i) for i in flds if i is not None])‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``

Side note: If all your fields were string you could also use " ".join(filter(None, flds)).  You can still use this by changing the following; flds = [str(a),b,c,d,e].  I prefer the method above as you can also change the str(i) to str(i).strip() to remove any leading or training spaces that may be in your fields.

Calculate Field:

If you do not want to use the code block with a call, you can use the following as a calculate statement:

" ".join([str(i) for i in [!AddrPrimaryHighNo!, !StreetPreDrctnAbbrev!, !StreetName!, !StreetSuffixAbbrev!, !streetPostDrctnAbbrev!] if i is not None])

Highlighted
MVP Esteemed Contributor

Are you really getting values like:

1498.0 None AVENUE K NONE NONE

or is it

1498.0 None AVENUE K None None

Highlighted
Occasional Contributor

Josh,

I've since modified my data so the AddrPrimaryHighNo field is now a string, but for the following (when AddrPrimaryHighNo was a double):

1699<Null>220THST<Null>

When I use:

str(!AddrPrimaryHighNo!) + " " + str(!StreetPreDrctnAbbrev!) + " " + str(!StreetName!) + " " + str(!StreetSuffixAbbrev!) + " " + str(!StreetPostDrctnAbbrev!)