Skip to content

Instantly share code, notes, and snippets.

@hwidlund
Created June 22, 2016 23:13
Show Gist options
  • Select an option

  • Save hwidlund/2da5ed13f81f300f42ef3b9bd03603eb to your computer and use it in GitHub Desktop.

Select an option

Save hwidlund/2da5ed13f81f300f42ef3b9bd03603eb to your computer and use it in GitHub Desktop.
Snippet for concatenating parsed fields without extra spaces
# concatenate parsed address fields without extra spaces
import arcpy
featureClass = "c:\\myfakepath\\data.gdb\\fc"
# create an expression to find records where the STREETNO field is not blank
# this indicates an address to be concatenated
expression = "STREETNO IS NOT NULL"
# create a list of the fields to be concatenated
# (the last field in the list is the field to be concatenated in to)
fields = ["STREETNO","STREETDIR","STREETNAME","STREETSUF","STREETUNIT","STREETADDR"]
# create an update cursor limited to the fields in the fields list, and by the expression
try:
with arcpy.da.UpdateCursor(featureClass,fields,expression) as cursor:
for row in cursor:
if not row[1]: ## if streetdir is null
exp = "{0} {1}".format(str(row[0]),row[2]) ## streetno + streetname
else: ## streetdir is not null
exp = "{0} {1} {2}".format(row[0],row[1],row[2])## streetno + streetdir + streetname
if row[3]: ## if streetsuf is not null
exp += " {0}".format(row[3]) ## add streetsuf to exp
if row[4]: ## if streetunit is not null
exp += " {0}".format(row[4]) ## add streetunit to exp
row[5] = exp ## set streetaddr field = exp
cursor.updateRow(row)
except Exception as e:
error = "Failed: concat fields\n {0} {1}".format(arcpy.GetMessages(), str(e))
print(error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment