0
  • fields:

    • STNUM = double
    • STRDIR = string
    • STRNAME = string
    • STRTYPE = string

Tuple and s.format

I have tried:

arcpy.CalculateField_management (newparcelsUTM, "SiteAddress","{0} {1} {2} {3} ".format('!STRNUM!','!STRDIR!','!STRNAME!','!STRTYPE!'),"PYTHON_9.3")
## set variable- str(double) ##

I have tried setting a variable to change the double field to a string:

AddNum = str('!STNUM!')

then use it in the Calculate field line:

arcpy.CalculateField_management (newparcelsUTM, "SiteAddress", AddNum+" "+'!STRDIR!'+" "+'!STRNAME!'+" "+'!STRTYPE!'),"PYTHON_9.3")

All my attempts end with:

ExecuteError: ERROR 000539: SyntaxError: invalid syntax (, line 1) Failed to execute (CalculateField).

I have tried different iterations on this- single quotes v double quotes... it works if I use just the STNUM field. It works if I use any combination of the string fields. It just does not work combining the double field with the string fields! I have read many threads regarding concatenate, Field calculator, combine double with string, on stack exchange and the Esri sites... I have been trying!

2

You don't need quotes around the field names. format() will convert everything to strings for you so you don't have to do any conversions. For fields with NULLs, use !FIELD! or "" so it doesn't put a None in the address.

arcpy.CalculateField_management(
    newparcelsUTM, 
    "SiteAddress",
    '"{0} {1} {2} {3}".format(!STRNUM!, !STRDIR! or "", !STRNAME!, !STRTYPE!)',
     "PYTHON_9.3" 
)
  • when I remove the single quotes and try to run the script, then I get a new dialogue box that pops up with 'There's an error in your program: invalid syntax' and the first exclamation point on STRNUM is highlighted red. – Gross Dec 27 '18 at 19:54
  • Sorry, forgot to put the expression in quotes, see update – Marc Pfister Dec 27 '18 at 20:09
  • Those can be a pain - you should be able to get around it with an or "" to swap it with an empty string (see update) – Marc Pfister Dec 27 '18 at 21:43
3

You can use the da.UpdateCursor:

import arcpy
fc = r'C:\data.gdb\featureclass' #Change
fields = ['SiteAdress','STNUM','STRDIR','STRNAME','STRTYPE']
with arcpy.da.UpdateCursor(fc,fields) as cursor:
    for row in cursor:
        row[0] = ' '.join([i for i in row[1:] if i is not None]) 
        cursor.updateRow(row)
  • Thanks BERA- I forgot to mention that there are NULLs in some of the fields, so for some of them I get a return of 619 None Englebert Way... I am not fond of cursors- because of my lack of understanding!- but Marc's answer worked. – Gross Dec 27 '18 at 22:11
  • I have changed the code so it will work with nones – BERA Dec 28 '18 at 9:11
  • 1
    @Gross, cursors are much cleaner, more versatile, and more "pythonic". I recommend learning them. – Tom Dec 31 '18 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.