Concatenating Strings with Field Calculator and Python - dealing with NULLS

14448
20
Jump to solution
11-06-2015 11:08 AM
ChrisDonohue__GISP
MVP Alum

I have some addressing data that I would like to update and one of the tasks is to concatenate a new full street address field [ADDR1] by combining several of the component fields.  I know how to do this in VBScript, but figured this would be a good example to work out in Python, as I don't use Python often and have alot to learn.  My programming background is FORTRAN and BASIC, and in the years since those were popular hack and slash adapting code for AML/Avenue/VBA/VB.Net - so assume I know little about Python.

What is desired (psuedo-logic):

ADDR1 = STRNUM + STRNUMSUF + STRPREDIR + STRNAME + STRTYPE + STRSUFDIR

I'm using ArcGIS 10.2.1 and the data shown here is test data in a File Geodatabase feature class.

After poking around various Help articles, it appears that in Python the .format method in would work.  However, when I run it in Field Calculator the result looks good except "None" gets concatenated as text if the original field value was NULL (this issue doesn't come up with VBScript).  I'd like any values of NULL to instead just be blank - i.e. ignore them entirely.  For example, for the first record the desired outcome in ADDR1 is "224 D N CHURCH ST" not "224 D N CHURCH ST None".

Concatenation Python NULLS become None.png

One of the Python articles I found mentions the NULL issue:

Dealing with Null Values.jpg

Source:  Concatenating field values using Calculate Field | ArcGIS Blog

Is there a different way to do the concatenation in Python to achieve the desired outcome?  I'm trying to wrap my head around how Python approaches this, particularly in regards to using it in ArcMap in Field Calculator. 

  • I noticed some articles suggested casting all the fields to string to resolve the NULLS.  However, I haven't had any luck adding in str() in with the fields in the format statement shown.  It would just error out.  Would this need to be a separate line to do the casting?
  • Would a better approach be to use the Codeblock to run some sort of process to convert NULLS to blank before running the format method?  For example, adapt a process similar to this?

FixNull.jpg

Source:  arcgis desktop - Calculate Field tool to calculate on null fields - Geographic Information Systems S...

Lastly, can you recommend a good guide for beginners that explains how to use Python for these sort of issues?

Thanks,

Chris Donohue, GISP

20 Replies
Matt-Goodman
Occasional Contributor

This is one of the simplest, most useful things I've seen in a while. Thanks, @DanPatterson !

0 Kudos